Skip to content

Localize a column of Timestamps #2591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hayd opened this issue Dec 23, 2012 · 1 comment
Closed

Localize a column of Timestamps #2591

hayd opened this issue Dec 23, 2012 · 1 comment
Labels
Datetime Datetime data dtype Enhancement
Milestone

Comments

@hayd
Copy link
Contributor

hayd commented Dec 23, 2012

Migrated from StackOverflow:

 s = pd.Series(pd.date_range('2012-1-1 1:30', periods=3, freq='min', tz='EST'))

You can convert each item individually:

In [86]: s[0]
Out[86]: <Timestamp: 2012-01-01 06:30:00>

In [87]: s[0].tz_localize('UTC')
Out[87]: <Timestamp: 2012-01-01 06:30:00+0000 UTC, tz=UTC>

But when you try to apply across all columns (same with convert_dtype=True):

In [81]: s.apply(lambda x: x.tz_localize('UTC'), convert_dtype=False)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-81-0b4fa5c03650> in <module>()
----> 1 s.apply(lambda x: x.tz_localize('UTC'), convert_dtype=False)

/usr/lib/pymodules/python2.7/pandas/core/series.pyc in apply(self, func, convert_dtype, args, **kwds)
   2244             return f(self)
   2245 
-> 2246         mapped = lib.map_infer(self.values, f, convert=convert_dtype)
   2247         if isinstance(mapped[0], Series):
   2248             from pandas.core.frame import DataFrame

/usr/lib/pymodules/python2.7/pandas/lib.so in pandas.lib.map_infer (pandas/lib.c:39084)()

<ipython-input-81-0b4fa5c03650> in <lambda>(x)
----> 1 s.apply(lambda x: x.tz_localize('UTC'), convert_dtype=False)

AttributeError: 'numpy.datetime64' object has no attribute 'tz_localize'

One workaround is forcing it to be a Timestamp:

In [91]: from pandas.lib import Timestamp

In [92]: s.apply(lambda x: Timestamp(x).tz_localize('UTC')) 
Out[92]: 
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
@jreback
Copy link
Contributor

jreback commented Sep 20, 2013

Seems to be working now.....

In [8]: s = pd.Series(pd.date_range('2012-1-1 1:30', periods=3, freq='min', tz='EST'))

In [9]: s
Out[9]: 
0   2012-01-01 06:30:00
1   2012-01-01 06:31:00
2   2012-01-01 06:32:00
dtype: datetime64[ns]

In [10]: s.apply(lambda x: x.tz_localize('UTC'), convert_dtype=False)
Out[10]: 
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
dtype: object

In [11]: s.apply(lambda x: x.tz_localize('UTC'))
Out[11]: 
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
dtype: object

In [12]: s.apply(lambda x: x.tz_localize('UTC'))[0]
Out[12]: Timestamp('2012-01-01 06:30:00+0000', tz='UTC')

@jreback jreback closed this as completed Sep 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants