implementing Formatting DateTime in Pandas 1.5.0 When Using Timezones in Python 3.11
I'm sure I'm missing something obvious here, but I'm attempting to set up I've been struggling with this for a few days now and could really use some help. Hey everyone, I'm running into an issue that's driving me crazy... I'm having trouble with formatting a DateTime column in a Pandas DataFrame that contains timezone-aware timestamps. When I try to format these timestamps to a string representation, I encounter unexpected results. Here's a small snippet of my code: ```python import pandas as pd from datetime import datetime as dt import pytz # Creating a DataFrame with timezone-aware DateTime data = { 'timestamp': [ dt(2023, 10, 1, 12, 0, tzinfo=pytz.UTC), dt(2023, 10, 2, 12, 0, tzinfo=pytz.UTC) ] } df = pd.DataFrame(data) # Attempting to format the DateTime column formatted_times = df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S') print(formatted_times) ``` When I run the code, I expect to see the timestamps formatted correctly as strings, but instead, I get the following output: ``` 0 2023-10-01 12:00:00+00:00 1 2023-10-02 12:00:00+00:00 Name: timestamp, dtype: object ``` It seems like the timezone information is not being stripped or formatted as I want. I tried using `dt.tz_localize(None)` before formatting, but that leads to a `ValueError` since the timestamps are already timezone-aware. Hereโs what I tried: ```python # This raises a ValueError formatted_times = df['timestamp'].dt.tz_localize(None).dt.strftime('%Y-%m-%d %H:%M:%S') ``` I also checked the documentation for any changes related to timezone handling in Pandas 1.5.0 but couldnโt find anything relevant. Can anyone suggest how to properly format these timezone-aware DateTime objects to get a plain string output without the timezone information? Thanks in advance! What's the best practice here? Has anyone else encountered this? The project is a desktop app built with Python. Could this be a known issue? I'm developing on Windows 10 with Python. Any ideas what could be causing this?