Ernesto561
Ernesto561

Reputation: 577

Strip timezone info in pandas

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?

Upvotes: 34

Views: 38916

Answers (6)

Galuoises
Galuoises

Reputation: 3283

If you want to remove all timezones from all datetime64 columns you could use this

for col in df.select_dtypes(include=['datetime64[ns, UTC]']).columns:
    df[col] = df[col].apply(lambda x: x.tz_localize(None))
    

Upvotes: 0

Mikhail
Mikhail

Reputation: 799

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)

Upvotes: 9

ryanjdillon
ryanjdillon

Reputation: 18978

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))

Upvotes: 4

Diego Mora Cespedes
Diego Mora Cespedes

Reputation: 3872

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.

Upvotes: 46

jezrael
jezrael

Reputation: 863166

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

Upvotes: 13

Caridorc
Caridorc

Reputation: 6661

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'

Upvotes: -1

Related Questions