Pandas time zone information

I struggled with deleting timezone information from a column in the pandas framework. I checked the following question, but for me this does not work:

Is it possible to export pandas DataFrame to Excel to remove tzinfo?

I used tz_localize to assign a time zone to a datetime object, because I need to convert to a different time zone using tz_convert. This adds the UTC offset, in the order of "-06: 00". I need to get rid of this bias because it leads to an error when trying to export a DataFrame to Excel.

Actual output

2015-12-01 00:00:00-06:00 

Required conclusion

 2015-12-01 00:00:00 

I tried to get the characters that I want to use with the str () method, but it seems the result of tz_localize is not a string. My solution so far is to export the dataframe to csv, read the file, and use the str () method to get the characters I want.

Is there an easier solution?

+5
source share
3 answers

The strip of the last 6 characters can help:

 print df datetime 0 2015-12-01 00:00:00-06:00 1 2015-12-01 00:00:00-06:00 2 2015-12-01 00:00:00-06:00 df['datetime'] = df['datetime'].astype(str).str[:-6] print df datetime 0 2015-12-01 00:00:00 1 2015-12-01 00:00:00 2 2015-12-01 00:00:00 
+5
source

If your series contains only dates, you can do:

my_series.dt.tz_localize(None)

This will delete the time zone information (it will not change the time) and return a series of naive local times that can be exported to excel using to_excel (), for example.

+3
source

If it's always the last 6 characters you want to ignore, you can simply slice the current line:

 >>> '2015-12-01 00:00:00-06:00'[0:-6] '2015-12-01 00:00:00' 
0
source

Source: https://habr.com/ru/post/1239405/


All Articles