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.,
s=pd.Series(pd.date_range('2012-1-1 1:30',periods=3,freq='min'))
How to convert s
to UTC?
(Note that I don't actually use date_range()
so using its tz
parameter is not an option.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With