Skip to content
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

BUG: pandas Timestamp tz_localize and tz_convert do not preserve freq attribute #25247

Merged
merged 14 commits into from
Feb 11, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Timezones
^^^^^^^^^

- Bug in :func:`to_datetime` with ``utc=True`` and datetime strings that would apply previously parsed UTC offsets to subsequent arguments (:issue:`24992`)
-
- Bug in :func:`Timestamp.tz_localize` and :func:`Timestamp.tz_convert` does not propagate ``freq`` (:issue:`25241`)
-

Numeric
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1187,12 +1187,12 @@ class Timestamp(_Timestamp):
value = tz_localize_to_utc(np.array([self.value], dtype='i8'), tz,
ambiguous=ambiguous,
nonexistent=nonexistent)[0]
return Timestamp(value, tz=tz)
return Timestamp(value, tz=tz, freq=self.freq)
else:
if tz is None:
# reset tz
value = tz_convert_single(self.value, UTC, self.tz)
return Timestamp(value, tz=None)
return Timestamp(value, tz=tz, freq=self.freq)
else:
raise TypeError('Cannot localize tz-aware Timestamp, use '
'tz_convert for conversions')
Expand Down Expand Up @@ -1222,7 +1222,7 @@ class Timestamp(_Timestamp):
'tz_localize to localize')
else:
# Same UTC timestamp, different time zone
return Timestamp(self.value, tz=tz)
return Timestamp(self.value, tz=tz, freq=self.freq)

astimezone = tz_convert

Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pandas.errors import OutOfBoundsDatetime
import pandas.util._test_decorators as td

from pandas import NaT, Period, Timedelta, Timestamp
from pandas import DatetimeIndex, NaT, Period, Timedelta, Timestamp
import pandas.util.testing as tm

from pandas.tseries import offsets
Expand Down Expand Up @@ -780,6 +780,19 @@ def test_hash_equivalent(self):
stamp = Timestamp(datetime(2011, 1, 1))
assert d[stamp] == 5

def test_tz_conversion_freq(self, tz_naive_fixture):
# GH25241
t1 = Timestamp('2019-01-01 10:00', freq='H')
assert t1.tz_localize(tz=tz_naive_fixture).freq == t1.freq
t2 = Timestamp('2019-01-02 12:00', tz='UTC', freq='T')
assert t2.tz_convert(tz='UTC').freq == t2.freq

# freq propagation error doesn't happen with DatetimeIndex
t3 = DatetimeIndex(['2019-01-01 10:00'], freq='H')
Copy link
Member

Choose a reason for hiding this comment

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

These are better suited in pandas/tests/indexes/datetimes/test_timezones.py. After moved, LGTM

Copy link
Member Author

Choose a reason for hiding this comment

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

moved! thanks! @mroeschke

assert t3.tz_localize(tz=tz_naive_fixture).freq == t3.freq
t4 = DatetimeIndex(['2019-01-02 12:00'], tz='UTC', freq='T')
assert t4.tz_convert(tz='UTC').freq == t4.freq


class TestTimestampNsOperations(object):

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def test_getitem_get(test_data):

# missing
d = test_data.ts.index[0] - BDay()
with pytest.raises(KeyError, match=r"Timestamp\('1999-12-31 00:00:00'\)"):
msg = r"Timestamp\('1999-12-31 00:00:00', freq='B'\)"
with pytest.raises(KeyError, match=msg):
test_data.ts[d]

# None
Expand Down