How to convert the time zone of the values of a Pandas Series

前端 未结 1 385
日久生厌
日久生厌 2021-02-06 06:37

I have a pandas Series with values of type datetime64[ns]. The dates are in EST timezone, and I would like to convert them to UTC timezone.

E.g.,

         


        
相关标签:
1条回答
  • 2021-02-06 07:07

    Update: In recent pandas, you can use the dt accessor to broadcast this:

    In [11]: s.dt.tz_localize('UTC')
    Out[11]:
    0   2012-01-01 01:30:00+00:00
    1   2012-01-01 01:31:00+00:00
    2   2012-01-01 01:32:00+00:00
    dtype: datetime64[ns, UTC]
    

    Here's one way (depending if tz is already set it might be a tz_convert rather than tz_localize):

    In [21]: from pandas.lib import Timestamp
    
    In [22]: s.apply(lambda x: x.tz_localize('UTC')) 
    Out[22]: 
    0    2012-01-01 06:30:00+00:00
    1    2012-01-01 06:31:00+00:00
    2    2012-01-01 06:32:00+00:00
    
    0 讨论(0)
提交回复
热议问题