Skip to content

BUG: DatetimeIndex.shift(1) with empty index #36691

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
Sep 30, 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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)
- Bug in :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with ``datetime64``, ``timedelta64`` or ``Period`` dtype placement of ``NaT`` values being inconsistent with ``NumPy`` (:issue:`36176`, :issue:`36254`)
- Inconsistency in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` setitem casting arrays of strings to datetimelike scalars but not scalar strings (:issue:`36261`)
-
- Bug in :class:`DatetimeIndex.shift` incorrectly raising when shifting empty indexes (:issue:`14811`)


Timedelta
^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,8 +1276,8 @@ def _time_shift(self, periods, freq=None):
result = self + offset
return result

if periods == 0:
# immutable so OK
if periods == 0 or len(self) == 0:
# GH#14811 empty case
return self.copy()

if self.freq is None:
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def test_shift_identity(self):
idx = self.create_index()
tm.assert_index_equal(idx, idx.shift(0))

def test_shift_empty(self):
# GH#14811
idx = self.create_index()[:0]
tm.assert_index_equal(idx, idx.shift(1))

def test_str(self):

# test the string repr
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/datetimes/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ def test_shift_bmonth(self):
with tm.assert_produces_warning(pd.errors.PerformanceWarning):
shifted = rng.shift(1, freq=pd.offsets.CDay())
assert shifted[0] == rng[0] + pd.offsets.CDay()

def test_shift_empty(self):
# GH#14811
dti = date_range(start="2016-10-21", end="2016-10-21", freq="BM")
result = dti.shift(1)
tm.assert_index_equal(result, dti)