Skip to content

Commit 78bca00

Browse files
authored
BUG: DatetimeIndex.shift(1) with empty index (pandas-dev#36691)
1 parent 0fc20dd commit 78bca00

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ Datetimelike
252252
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)
253253
- 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`)
254254
- Inconsistency in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` setitem casting arrays of strings to datetimelike scalars but not scalar strings (:issue:`36261`)
255-
-
255+
- Bug in :class:`DatetimeIndex.shift` incorrectly raising when shifting empty indexes (:issue:`14811`)
256+
256257

257258
Timedelta
258259
^^^^^^^^^

pandas/core/arrays/datetimelike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,8 @@ def _time_shift(self, periods, freq=None):
12761276
result = self + offset
12771277
return result
12781278

1279-
if periods == 0:
1280-
# immutable so OK
1279+
if periods == 0 or len(self) == 0:
1280+
# GH#14811 empty case
12811281
return self.copy()
12821282

12831283
if self.freq is None:

pandas/tests/indexes/datetimelike.py

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def test_shift_identity(self):
3232
idx = self.create_index()
3333
tm.assert_index_equal(idx, idx.shift(0))
3434

35+
def test_shift_empty(self):
36+
# GH#14811
37+
idx = self.create_index()[:0]
38+
tm.assert_index_equal(idx, idx.shift(1))
39+
3540
def test_str(self):
3641

3742
# test the string repr

pandas/tests/indexes/datetimes/test_shift.py

+6
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,9 @@ def test_shift_bmonth(self):
151151
with tm.assert_produces_warning(pd.errors.PerformanceWarning):
152152
shifted = rng.shift(1, freq=pd.offsets.CDay())
153153
assert shifted[0] == rng[0] + pd.offsets.CDay()
154+
155+
def test_shift_empty(self):
156+
# GH#14811
157+
dti = date_range(start="2016-10-21", end="2016-10-21", freq="BM")
158+
result = dti.shift(1)
159+
tm.assert_index_equal(result, dti)

0 commit comments

Comments
 (0)