Skip to content

ENH: Add tz_localize and tz_convert to Series.dt methods #8187

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
TomAugspurger opened this issue Sep 5, 2014 · 6 comments · Fixed by #8184
Closed

ENH: Add tz_localize and tz_convert to Series.dt methods #8187

TomAugspurger opened this issue Sep 5, 2014 · 6 comments · Fixed by #8184
Labels
API Design Datetime Datetime data dtype Timezones Timezone data dtype
Milestone

Comments

@TomAugspurger
Copy link
Contributor

In [6]: s = pd.Series(pd.date_range(start='2014-01-01', periods=30, freq='d'))

In [7]: s.dt.tz_localize('US/Central')

Would return a Series with the same index whose values are Timestamps with the timezone set.

I haven't looked at this code too closely, so I'm not sure how difficult it would be and if it's doable for 0.15.

@hayd brought it up on SO: http://stackoverflow.com/questions/25656826/speeding-up-timestamp-operations/25657249?noredirect=1#comment40118386_25657249

@TomAugspurger TomAugspurger added this to the 0.15.0 milestone Sep 5, 2014
@jorisvandenbossche
Copy link
Member

The only thing is that the result is no longer a datetime64 column (but maybe we could just warn for that in the docstring?). But of course this is the same with the round-trip through DatetimeIndex.

Furthermore, I think to_pydatetime and to_period would also be useful to add to .dt

@hayd
Copy link
Contributor

hayd commented Sep 7, 2014

so datetime columns with tz become object even if they share tz info (I'd never noticed that!)... is this another case of wanting Series meta data or is there some way we can have dti behaviour here?

In [11]: s = pd.Series(pd.date_range(start='2014-01-01', periods=30, freq='d'));

In [12]: pd.Series(pd.DatetimeIndex(s).tz_localize('US/Central')).head()
Out[12]:
0    2014-01-01 00:00:00-06:00
1    2014-01-02 00:00:00-06:00
2    2014-01-03 00:00:00-06:00
3    2014-01-04 00:00:00-06:00
4    2014-01-05 00:00:00-06:00
dtype: object

+1 on method parity between dti & dt.

@jreback
Copy link
Contributor

jreback commented Sep 7, 2014

it IS possible to fix this and make datetime tz aware columns

need a subclass of the DatetimeBlock internally that keeps the tz (would need to be a uniform tz for the block)

not sure how tricky his would be

@ischwabacher
Copy link
Contributor

The vast majority of the friction I've experienced using pandas has come from the fact that Timestamps are Good and datetime64s are Evil, and which representation my data have at a given time is a pandas sort-of-internal that leaks all over the place. Anything that reduces that friction gets a +1 from me.

@jreback
Copy link
Contributor

jreback commented Sep 8, 2014

yep sort of trivial, going to add as part of #8184 (as well as to_period/to_pydatetime)

@jreback
Copy link
Contributor

jreback commented Sep 8, 2014

In [5]: s = Series(date_range('20130101',periods=3))

In [6]: s
Out[6]: 
0   2013-01-01
1   2013-01-02
2   2013-01-03
dtype: datetime64[ns]

In [7]: s.dt.
s.dt.date              s.dt.dayofyear         s.dt.is_month_start    s.dt.is_year_end       s.dt.minute            s.dt.quarter           s.dt.to_period         s.dt.tz_localize       s.dt.weekofyear        
s.dt.day               s.dt.hour              s.dt.is_quarter_end    s.dt.is_year_start     s.dt.month             s.dt.second            s.dt.to_pydatetime     s.dt.week              s.dt.year              
s.dt.dayofweek         s.dt.is_month_end      s.dt.is_quarter_start  s.dt.microsecond       s.dt.nanosecond        s.dt.time              s.dt.tz_convert        s.dt.weekday           

In [7]: s.dt.tz_localize('US/Eastern')
Out[7]: 
0    2013-01-01 00:00:00-05:00
1    2013-01-02 00:00:00-05:00
2    2013-01-03 00:00:00-05:00
dtype: object

# this returns a scalar (as opposed to a Series)
In [4]: s.dt.tz_localize('US/Eastern').dt.tz
Out[4]: <DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants