Strip timezone info in pandas

前端 未结 5 1419
一生所求
一生所求 2020-12-09 17:04

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

相关标签:
5条回答
  • 2020-12-09 17:07

    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'
    
    0 讨论(0)
  • 2020-12-09 17:09

    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.

    0 讨论(0)
  • 2020-12-09 17:12

    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))
    
    0 讨论(0)
  • 2020-12-09 17:20

    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
    
    0 讨论(0)
  • 2020-12-09 17:28

    To remove timezone from all columns just use:

    for col in df.select_dtypes(['datetimetz']).columns:
        df[col] = df[col].dt.tz_convert(None)
    
    0 讨论(0)
提交回复
热议问题