Skip to content

Commit 50ee2ee

Browse files
charlesdong1991Pingviinituutti
authored andcommitted
BUG: pandas Timestamp tz_localize and tz_convert do not preserve freq attribute (pandas-dev#25247)
1 parent 5ebc62b commit 50ee2ee

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Timezones
9595
^^^^^^^^^
9696

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

101101
Numeric

pandas/_libs/tslibs/timestamps.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1187,12 +1187,12 @@ class Timestamp(_Timestamp):
11871187
value = tz_localize_to_utc(np.array([self.value], dtype='i8'), tz,
11881188
ambiguous=ambiguous,
11891189
nonexistent=nonexistent)[0]
1190-
return Timestamp(value, tz=tz)
1190+
return Timestamp(value, tz=tz, freq=self.freq)
11911191
else:
11921192
if tz is None:
11931193
# reset tz
11941194
value = tz_convert_single(self.value, UTC, self.tz)
1195-
return Timestamp(value, tz=None)
1195+
return Timestamp(value, tz=tz, freq=self.freq)
11961196
else:
11971197
raise TypeError('Cannot localize tz-aware Timestamp, use '
11981198
'tz_convert for conversions')
@@ -1222,7 +1222,7 @@ class Timestamp(_Timestamp):
12221222
'tz_localize to localize')
12231223
else:
12241224
# Same UTC timestamp, different time zone
1225-
return Timestamp(self.value, tz=tz)
1225+
return Timestamp(self.value, tz=tz, freq=self.freq)
12261226

12271227
astimezone = tz_convert
12281228

pandas/tests/indexes/datetimes/test_timezones.py

+7
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,13 @@ def test_dti_drop_dont_lose_tz(self):
825825

826826
assert ind.tz is not None
827827

828+
def test_dti_tz_conversion_freq(self, tz_naive_fixture):
829+
# GH25241
830+
t3 = DatetimeIndex(['2019-01-01 10:00'], freq='H')
831+
assert t3.tz_localize(tz=tz_naive_fixture).freq == t3.freq
832+
t4 = DatetimeIndex(['2019-01-02 12:00'], tz='UTC', freq='T')
833+
assert t4.tz_convert(tz='UTC').freq == t4.freq
834+
828835
def test_drop_dst_boundary(self):
829836
# see gh-18031
830837
tz = "Europe/Brussels"

pandas/tests/scalar/timestamp/test_timestamp.py

+7
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,13 @@ def test_hash_equivalent(self):
780780
stamp = Timestamp(datetime(2011, 1, 1))
781781
assert d[stamp] == 5
782782

783+
def test_tz_conversion_freq(self, tz_naive_fixture):
784+
# GH25241
785+
t1 = Timestamp('2019-01-01 10:00', freq='H')
786+
assert t1.tz_localize(tz=tz_naive_fixture).freq == t1.freq
787+
t2 = Timestamp('2019-01-02 12:00', tz='UTC', freq='T')
788+
assert t2.tz_convert(tz='UTC').freq == t2.freq
789+
783790

784791
class TestTimestampNsOperations(object):
785792

pandas/tests/series/indexing/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def test_getitem_get(test_data):
114114

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

120121
# None

0 commit comments

Comments
 (0)