Skip to content

BUG+TST: non-optimized apply_index and empty DatetimeIndex #30336

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 3 commits into from
Dec 20, 2019
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.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ Datetimelike
- Bug in :func:`pandas.to_datetime` when called with ``None`` raising ``TypeError`` instead of returning ``NaT`` (:issue:`30011`)
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
- Bug in :meth:`Series.item` with ``datetime64`` or ``timedelta64`` dtype, :meth:`DatetimeIndex.item`, and :meth:`TimedeltaIndex.item` returning an integer instead of a :class:`Timestamp` or :class:`Timedelta` (:issue:`30175`)
-
- Bug in :class:`DatetimeIndex` addition when adding a non-optimized :class:`DateOffset` incorrectly dropping timezone information (:issue:`30336`)

Timedelta
^^^^^^^^^
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,16 +794,17 @@ def _add_offset(self, offset):
values = self.tz_localize(None)
else:
values = self
result = offset.apply_index(values)
if self.tz is not None:
result = result.tz_localize(self.tz)
result = offset.apply_index(values).tz_localize(self.tz)

except NotImplementedError:
warnings.warn(
"Non-vectorized DateOffset being applied to Series or DatetimeIndex",
PerformanceWarning,
)
result = self.astype("O") + offset
if len(self) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

use not len

Copy link
Member Author

Choose a reason for hiding this comment

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

follow-up, ive got a branch specifically collecting these things

# _from_sequence won't be able to infer self.tz
return type(self)._from_sequence(result).tz_localize(self.tz)

return type(self)._from_sequence(result, freq="infer")

Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pandas._libs.tslibs.offsets import ApplyTypeError
import pandas.compat as compat
from pandas.compat.numpy import np_datetime64_compat
from pandas.errors import PerformanceWarning

from pandas.core.indexes.datetimes import DatetimeIndex, _to_M8, date_range
from pandas.core.series import Series
Expand All @@ -43,7 +44,10 @@
CBMonthBegin,
CBMonthEnd,
CDay,
CustomBusinessDay,
CustomBusinessHour,
CustomBusinessMonthBegin,
CustomBusinessMonthEnd,
DateOffset,
Day,
Easter,
Expand Down Expand Up @@ -607,6 +611,46 @@ def test_add(self, offset_types, tz_naive_fixture):
assert isinstance(result, Timestamp)
assert result == expected_localize

def test_add_empty_datetimeindex(self, offset_types, tz_naive_fixture):
# GH#12724, GH#30336
offset_s = self._get_offset(offset_types)

dti = DatetimeIndex([], tz=tz_naive_fixture)

warn = None
if isinstance(
offset_s,
(
Easter,
WeekOfMonth,
LastWeekOfMonth,
CustomBusinessDay,
BusinessHour,
CustomBusinessHour,
CustomBusinessMonthBegin,
CustomBusinessMonthEnd,
FY5253,
FY5253Quarter,
),
):
# We don't have an optimized apply_index
warn = PerformanceWarning

with tm.assert_produces_warning(warn):
result = dti + offset_s
tm.assert_index_equal(result, dti)
with tm.assert_produces_warning(warn):
result = offset_s + dti
tm.assert_index_equal(result, dti)

dta = dti._data
with tm.assert_produces_warning(warn):
result = dta + offset_s
tm.assert_equal(result, dta)
with tm.assert_produces_warning(warn):
result = offset_s + dta
tm.assert_equal(result, dta)

def test_pickle_v0_15_2(self, datapath):
offsets = {
"DateOffset": DateOffset(years=1),
Expand Down