Skip to content
Advertisement

Why is pd.to_datetime() only changing type if utc is True?

After loading my csv file into my notebook in VS Code i wanted to change the columns type from object to datetime for some columns. So i did the following:

object values of columns

These are example values in the columns.

JavaScript

check

JavaScript

convert

JavaScript

check

After converting the columns i wanted to check if everything worked out fine.

JavaScript

This is strange. It seems my_col_3 and my_col_4 were not converted. But at the same time no Issue or Error was raised also while processing the code. my_col_3 and my_col_4 have the exact same format as my_col_1, yet they were not converted.

second approach

lets change the utc=Falseto utc=True

JavaScript

check

lets check again.

JavaScript

now i have

  • my_col_1 with UTC+01:00
  • my_col_3 with UTC+00:00 (turns 15:00:00 to 14:00:00 for example)
  • my_col_4 with UTC+00:00 (the same)

Question

This is strange. I do not want my_col_3 and my_col_4 to convert to the UTC+00:00 Timezone because i live in UTC+01:00.

Do i have to introduce another process step and re-apply my Timezone once again to these columns? But it seems redundant. Why not simply use the timezone already given in the columns like pandas does in my_col_1? Can i tell pd.to_datetime() what my timezone is? like utc=Truebut with tz=01:00?

Advertisement

Answer

So here is my Solution. Converting it with dt.tz_convert('Europe/Berlin') does the job. So i guess there is no way around to just take the already given +01:00 value. Turn the Column with to_datetime to UTC +00:00and afterwards you have to convert it to your Timezone again.

JavaScript

now my columns have changed into datetime with right Timezone

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement