4

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?

2
  • 1
    your input has multiple UTC offsets (not time zones, to be precise). A pandas Series of the pandas datetime type however can only handle one UTC offset or one specific time zone. That's why pd.to_datetime returns a pd.Series of Python datetime objects here (not pandas datetime) - which does not allow to use the dt accessor. Commented Dec 4, 2021 at 15:17
  • Great explanation of why it was doing this @MrFuppes - thanks Commented Dec 4, 2021 at 19:34

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.