32

I have been struggling with removing the time zone info from a column in a pandas dataframe. I have checked the following question, but it does not work for me:

Can I export pandas DataFrame to Excel stripping tzinfo?

I used tz_localize to assign a timezone to a datetime object, because I need to convert to another timezone using tz_convert. This adds an UTC offset, in the way "-06:00". I need to get rid of this offset, because it results in an error when I try to export the dataframe to Excel.

Actual output

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

Desired output

2015-12-01 00:00:00

I have tried to get the characters I want using 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 to use the str() method to get the characters I want.

Is there an easier solution?

Community
  • 1
  • 1
Ernesto561
  • 557
  • 1
  • 6
  • 11
  • 1
    doesn't `df['datetime'].dt.tz_localize(None)` work? replace `datetime` with what ever your column name is – EdChum Dec 29 '15 at 10:37
  • Thanks. It doesn´t work. The format of the date is "2015-12-01 00:00:00-06:00". I used "to_datetime" to convert the original date format to a datetime object, in order to apply "tz_localize" to convert to another time zone. It seems tz_localize adds that offset and I have not found how to get rid of it. – Ernesto561 Dec 29 '15 at 12:00
  • closely related (if not dupe) [Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone](https://stackoverflow.com/q/16628819/10197418) – FObersteiner Apr 14 '21 at 13:49

5 Answers5

44

If your series contains only datetimes, then you can do:

my_series.dt.tz_localize(None)

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

Diego Mora Cespedes
  • 3,605
  • 5
  • 26
  • 33
  • 3
    What if the pandas dataframe contains columns that are other than datetimes? I get errors like "TypeError: index is not a valid DatetimeIndex or PeriodIndex" – Dave X Mar 09 '18 at 15:53
  • 3
    @DaveX then you would do `my_df[time_columns] = my_df[time_columns].dt.tz_localize(None)` where `time_columns` is a list of column names that have datetime dtype – spencerlyon2 Mar 01 '19 at 19:15
13

Maybe help strip last 6 chars:

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
9

To remove timezone from all datetime columns in a DataFrame with mixed columns just use:

for col in df.select_dtypes(['datetimetz']).columns:
    df[col] = df[col].dt.tz_localize(None)

if you can't save df to excel file just use this (not delete timezone!):

for col in df.select_dtypes(['datetimetz']).columns:
    df[col] = df[col].dt.tz_convert(None)
Mikhail
  • 729
  • 1
  • 8
  • 16
  • 2
    Wrong answer: `tz_convert(None)` will convert to UTC prior to dropping the timezone indicator, so that `pd.Timestamp('2015-12-01 00:00:00-06:00').tz_convert(None)` will result in `Timestamp('2015-12-01 06:00:00')`. – Peter Mar 23 '21 at 20:22
4

Following Beatriz Fonseca's suggestion, I ended up doing the following:

from datetime import datetime
df['dates'].apply(lambda x:datetime.replace(x,tzinfo=None))
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110
-1

If it is always the last 6 characters that you want to ignore, you may simply slice your current string:

>>> '2015-12-01 00:00:00-06:00'[0:-6]
'2015-12-01 00:00:00'
Caridorc
  • 6,222
  • 2
  • 31
  • 46