Skip to content
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:`tz_localize` and :func:`tz_convert` does not propagate freq if ``Timestamp`` is initiated with freq (:issue:`25241`)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you specify Timestamp.tz_localize and Timestamp.tz_convert? Then you can remove the additional Timestamp towards the end.

Double back ticks around freq as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, thanks for your review! @mroeschke

-

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
7 changes: 7 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,13 @@ def test_hash_equivalent(self):
stamp = Timestamp(datetime(2011, 1, 1))
assert d[stamp] == 5

def test_tz_convert_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') == t2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you need to assert that the freq attributes are equal, as opposed to comparing the timestamps.

Timestamp freq values do not influence equality comparisons or hash values. Here's a quick example that shows this:

>>> t1 = pandas.Timestamp('2018-01-01 10:00', freq='H') 
>>> t2 = pandas.Timestamp('2018-01-01 10:00', freq='T') 
>>> t1 == t2
True
>>> t1.freq == t2.freq
False

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ahh, nice catch!!

t1 = pd.Timestamp('2018-01-01 10:00', freq='H')
t2 = pd.Timestamp('2018-01-01 10:00')])

and t1 != t2! thanks! @nmusolino



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