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:
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 UTCDatetimeIndex
ifTrue
(converting any tz-awaredatetime.datetime
objects as well).
df_vitals.Date_time = pd.to_datetime(df_vitals.Date_time, utc=True)