Skip to content

BUG: tz_localize needs to invalidate freq #33553

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 4 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ Datetimelike
- Bug where :meth:`PeriodIndex` raised when passed a :class:`Series` of strings (:issue:`26109`)
- Bug in :class:`Timestamp` arithmetic when adding or subtracting a ``np.ndarray`` with ``timedelta64`` dtype (:issue:`33296`)
- Bug in :meth:`DatetimeIndex.to_period` not infering the frequency when called with no arguments (:issue:`33358`)

- Bug in :meth:`DatetimeIndex.tz_localize` incorrectly retaining ``freq`` in some cases where the original freq is no longer valid (:issue:`30511`)

Timedelta
^^^^^^^^^
Expand Down
15 changes: 12 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,15 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
DatetimeIndex(['2018-03-01 09:00:00-05:00',
'2018-03-02 09:00:00-05:00',
'2018-03-03 09:00:00-05:00'],
dtype='datetime64[ns, US/Eastern]', freq='D')
dtype='datetime64[ns, US/Eastern]', freq=None)

With the ``tz=None``, we can remove the time zone information
while keeping the local time (not converted to UTC):

>>> tz_aware.tz_localize(None)
DatetimeIndex(['2018-03-01 09:00:00', '2018-03-02 09:00:00',
'2018-03-03 09:00:00'],
dtype='datetime64[ns]', freq='D')
dtype='datetime64[ns]', freq=None)

Be careful with DST changes. When there is sequential data, pandas can
infer the DST time:
Expand Down Expand Up @@ -973,7 +973,16 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
)
new_dates = new_dates.view(DT64NS_DTYPE)
dtype = tz_to_dtype(tz)
return self._simple_new(new_dates, dtype=dtype, freq=self.freq)

freq = None
if timezones.is_utc(tz) or (len(self) == 1 and not isna(new_dates[0])):
# we can preserve freq
# TODO: Also for fixed-offsets
freq = self.freq
elif tz is None and self.tz is None:
# no-op
freq = self.freq
return self._simple_new(new_dates, dtype=dtype, freq=freq)

# ----------------------------------------------------------------
# Conversion Methods - Vectorized analogues of Timestamp methods
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,3 +1161,26 @@ def test_iteration_preserves_nanoseconds(self, tz):
)
for i, ts in enumerate(index):
assert ts == index[i]


def test_tz_localize_invalidates_freq():
# we only preserve freq in unambiguous cases

# if localized to US/Eastern, this crosses a DST transition
dti = date_range("2014-03-08 23:00", "2014-03-09 09:00", freq="H")
assert dti.freq == "H"

result = dti.tz_localize(None) # no-op
assert result.freq == "H"

result = dti.tz_localize("UTC") # unambiguous freq preservation
assert result.freq == "H"

result = dti.tz_localize("US/Eastern", nonexistent="shift_forward")
assert result.freq is None
assert result.inferred_freq is None # i.e. we are not _too_ strict here

# Case where we _can_ keep freq because we're length==1
dti2 = dti[:1]
result = dti2.tz_localize("US/Eastern")
assert result.freq == "H"
1 change: 1 addition & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def test_ser_cmp_result_names(self, names, op):

# datetime64tz dtype
dti = dti.tz_localize("US/Central")
dti._set_freq("infer") # freq not preserved by tz_localize
ser = Series(dti).rename(names[1])
result = op(ser, dti)
assert result.name == names[2]
Expand Down