Skip to content

ENH: tz_localize(None) allows to reset tz #7852

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

Merged
merged 1 commit into from
Aug 5, 2014

Conversation

sinhrks
Copy link
Member

@sinhrks sinhrks commented Jul 26, 2014

Closes #7812. Allow tz_localize(None) for tz-aware Timestamp and DatetimeIndex to reset tz.
CC @rockg, @nehalecky

t = pd.Timestamp('2014-01-01 09:00', tz='Asia/Tokyo')

# tz_localize resets tz holding current tz representation
t.tz_localize(None)
#2014-01-01 09:00:00

# tz_convert reset tz using UTC representation (no changes by this PR)
t.tz_convert(None)
#2014-01-01 00:00:00

idx = pd.date_range(start='2011-01-01 09:00', freq='H', periods=10, tz='Asia/Tokyo')

idx.tz_localize(None)
# <class 'pandas.tseries.index.DatetimeIndex'
# [2011-01-01 09:00:00, ..., 2011-01-01 18:00:00]
# Length: 10, Freq: H, Timezone: None

#  (no changes by this PR)
idx.tz_convert(None)
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2011-01-01 00:00:00, ..., 2011-01-01 09:00:00]
# Length: 10, Freq: H, Timezone: None

@@ -1421,6 +1421,17 @@ to determine the right offset.
rng_hourly_eastern = rng_hourly.tz_localize('US/Eastern', infer_dst=True)
rng_hourly_eastern.values


To remove timezone from tz-aware ``DatetimeIndex``, use ``tz_localize(None)`` or ``tz_convert(None)``. ``tz_localize(None)`` will remove timezone holding local time representations. ``tz_convert(None)`` will remove timezone holding UTC time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so trying tz_convert(None) on a non-UTC does what? (why is this a special case)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tz_convert(None) removes timezone, returning an UTC time (see above example). This behavior is NOT changed by this PR, but not documented anywhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, maybe let's just document that (in the same section you just fixed, and the doc-strings as well).

thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, modified doc-strings also.

@jreback jreback added this to the 0.15.0 milestone Jul 26, 2014
@@ -824,6 +824,24 @@ def test_tz_localize_naive(self):

self.assertTrue(conv.equals(exp))

def test_tz_localize_roundtrip(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you test tz_convert here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for DatetimeIndex and Timestamp.

@nehalecky
Copy link
Contributor

idx.tz_localize(None) is the functionality I've been looking for. Just making sure I understand tz_convert(None), this would be equivalent to a chained .tz_convert('UTC').tz_localize(None)?

@jreback
Copy link
Contributor

jreback commented Jul 28, 2014

@nehalecky

In [2]: t = pd.Timestamp('2014-01-01 09:00', tz='Asia/Tokyo')

In [3]: t
Out[3]: Timestamp('2014-01-01 09:00:00+0900', tz='Asia/Tokyo')

In [4]: t.tz_convert(None)
Out[4]: Timestamp('2014-01-01 00:00:00')

In [5]: t.tz_convert('UTC')
Out[5]: Timestamp('2014-01-01 00:00:00+0000', tz='UTC')

In [6]: t.tz_convert('UTC').tz_localize(None)
Out[6]: Timestamp('2014-01-01 00:00:00')

In [7]: t.tz_convert('UTC').tz_convert(None)
Out[7]: Timestamp('2014-01-01 00:00:00')

In [8]: t.tz_localize(None)
Out[8]: Timestamp('2014-01-01 09:00:00')

tz_localize(None) essentially just drops the tz (but leaving the time as is), while convert
converts to UTC (first)

@nehalecky
Copy link
Contributor

@jreback thanks! Just like I understood. :)

@jorisvandenbossche
Copy link
Member

This also closes #3653
Nice to see a solution for this! (I initially asked it on stackoverflow in that issue)

@jorisvandenbossche
Copy link
Member

@sinhrks @jreback This is ready?

@jreback
Copy link
Contributor

jreback commented Aug 5, 2014

@sinhrks pls rebase and ping when green

good 2 go

@sinhrks
Copy link
Member Author

sinhrks commented Aug 5, 2014

@jreback rebased, and now green

jreback added a commit that referenced this pull request Aug 5, 2014
ENH: tz_localize(None) allows to reset tz
@jreback jreback merged commit 9b16d2e into pandas-dev:master Aug 5, 2014
@jreback
Copy link
Contributor

jreback commented Aug 5, 2014

thanks @sinhrks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Allow to reset tz from DatetimeIndex holding time representation
4 participants