Home > Back-end >  Parsing pandas DateTime where there are different timezones in dataframe
Parsing pandas DateTime where there are different timezones in dataframe

Time:12-04

I'm trying to parse a .csv file into a dataframe. The csv has multiple timezones because of daylight savings that happened during the recording of the data (ones at 01:00 others at 02:00). Here's a snippet for understanding:

enter image description here

After reading in the csv file, I have setup my code as follows:

df_vitals.Date_time = pd.to_datetime(df_vitals.Date_time, format ='%Y-%m-%d %H:%M:%S%z')
df_vitals.Date_time = df_vitals.Date_time.dt.tz_convert("Europe/Madrid")

Where Date_time is my column containing the mixed timezones. I get the following error:

AttributeError: Can only use .dt accessor with datetimelike values

Note that this works perfectly fine for my csv files with just one time zone (i.e. where no daylight savings happened)

How can I properly parse csv files that have more than one time zone in it?

CodePudding user response:

Instead of using format, set the utc param of to_datetime:

utc (boolean): Return UTC DatetimeIndex if True (converting any tz-aware datetime.datetime objects as well).

df_vitals.Date_time = pd.to_datetime(df_vitals.Date_time, utc=True)
  • Related