Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 571
Yariv Avatar asked Dec 22 '12 16:12

Yariv


1 Answers

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
like image 189
Andy Hayden Avatar answered Sep 23 '22 02:09

Andy Hayden