converting the timezone of a series of Timestamps

2,958 views
Skip to first unread message

Chris Withers

unread,
Nov 27, 2015, 5:00:33 AM11/27/15
to PyData
Hi All,

I feel like I must be missing something simple...

Say I have a series whose values are Timestamps:

>>> series = pd.Series(pd.date_range('2015-1-1 1:30', periods=3, freq='min'))

>>> series

0   2015-01-01 01:30:00

1   2015-01-01 01:31:00

2   2015-01-01 01:32:00

dtype: datetime64[ns]


If I want those values localised and then converted, I'd assume I could do:


>>> series.tz_localize('America/Chicago').tz_convert('Europe/London')

Traceback (most recent call last):

...

TypeError: index is not a valid DatetimeIndex or PeriodIndex


The closest I can find is:


>>> series.apply(lambda x: x.tz_localize('America/Chicago').tz_convert('Europe/London'), convert_dtype=False)

0    2015-01-01 07:30:00+00:00

1    2015-01-01 07:31:00+00:00

2    2015-01-01 07:32:00+00:00

dtype: object


But, as you can see, this changes the dtype. It's also painfully slow on any reasonable size frame.


What should I be doing?


Chris

tom

unread,
Nov 27, 2015, 7:54:06 AM11/27/15
to pyd...@googlegroups.com
Series.tz_localize / tz_convert operate on the index (checkout the docstring). You’ll want to use the `.dt` namespace

series.dt.tz_localize('America/Chicago’).dt.tz_convert('Europe/London')

I believe this requires pandas  0.17 or newer.

-Tom

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages