Skip to content

Commit

Permalink
BUG: pandas Timestamp tz_localize and tz_convert do not preserve `fre…
Browse files Browse the repository at this point in the history
…q` attribute (#25247)
  • Loading branch information
charlesdong1991 authored and jreback committed Feb 11, 2019
1 parent 75d5b48 commit 91992ad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
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 @@ -95,7 +95,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
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,13 @@ def test_dti_drop_dont_lose_tz(self):

assert ind.tz is not None

def test_dti_tz_conversion_freq(self, tz_naive_fixture):
# GH25241
t3 = DatetimeIndex(['2019-01-01 10:00'], freq='H')
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

def test_drop_dst_boundary(self):
# see gh-18031
tz = "Europe/Brussels"
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_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


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

0 comments on commit 91992ad

Please sign in to comment.