Skip to content

Commit 62bedfd

Browse files
sinhrksjreback
authored andcommitted
BUG/TST: Calling shift on a DatetimeIndex of length 0 returns an Index in…
closes #9903 Author: sinhrks <[email protected]> Closes #13026 from sinhrks/dtindex_shift and squashes the following commits: 0b39f52 [sinhrks] TST: Calling shift on a DatetimeIndex of length 0 returns an Index instead of a DatetimeIndex
1 parent b56cea2 commit 62bedfd

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

pandas/tseries/tests/test_base.py

+59
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,23 @@ def test_nat_new(self):
617617
exp = np.array([tslib.iNaT] * 5, dtype=np.int64)
618618
tm.assert_numpy_array_equal(result, exp)
619619

620+
def test_shift(self):
621+
# GH 9903
622+
for tz in [None, 'US/Eastern', 'Asia/Tokyo']:
623+
idx = pd.DatetimeIndex([], name='xxx', tz=tz)
624+
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
625+
tm.assert_index_equal(idx.shift(3, freq='H'), idx)
626+
627+
idx = pd.DatetimeIndex(['2011-01-01 10:00', '2011-01-01 11:00'
628+
'2011-01-01 12:00'], name='xxx', tz=tz)
629+
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
630+
exp = pd.DatetimeIndex(['2011-01-01 13:00', '2011-01-01 14:00'
631+
'2011-01-01 15:00'], name='xxx', tz=tz)
632+
tm.assert_index_equal(idx.shift(3, freq='H'), exp)
633+
exp = pd.DatetimeIndex(['2011-01-01 07:00', '2011-01-01 08:00'
634+
'2011-01-01 09:00'], name='xxx', tz=tz)
635+
tm.assert_index_equal(idx.shift(-3, freq='H'), exp)
636+
620637

621638
class TestTimedeltaIndexOps(Ops):
622639
def setUp(self):
@@ -1290,6 +1307,27 @@ def test_nat_new(self):
12901307
exp = np.array([tslib.iNaT] * 5, dtype=np.int64)
12911308
tm.assert_numpy_array_equal(result, exp)
12921309

1310+
def test_shift(self):
1311+
# GH 9903
1312+
idx = pd.TimedeltaIndex([], name='xxx')
1313+
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
1314+
tm.assert_index_equal(idx.shift(3, freq='H'), idx)
1315+
1316+
idx = pd.TimedeltaIndex(['5 hours', '6 hours', '9 hours'], name='xxx')
1317+
tm.assert_index_equal(idx.shift(0, freq='H'), idx)
1318+
exp = pd.TimedeltaIndex(['8 hours', '9 hours', '12 hours'], name='xxx')
1319+
tm.assert_index_equal(idx.shift(3, freq='H'), exp)
1320+
exp = pd.TimedeltaIndex(['2 hours', '3 hours', '6 hours'], name='xxx')
1321+
tm.assert_index_equal(idx.shift(-3, freq='H'), exp)
1322+
1323+
tm.assert_index_equal(idx.shift(0, freq='T'), idx)
1324+
exp = pd.TimedeltaIndex(['05:03:00', '06:03:00', '9:03:00'],
1325+
name='xxx')
1326+
tm.assert_index_equal(idx.shift(3, freq='T'), exp)
1327+
exp = pd.TimedeltaIndex(['04:57:00', '05:57:00', '8:57:00'],
1328+
name='xxx')
1329+
tm.assert_index_equal(idx.shift(-3, freq='T'), exp)
1330+
12931331

12941332
class TestPeriodIndexOps(Ops):
12951333
def setUp(self):
@@ -2103,3 +2141,24 @@ def test_nat_new(self):
21032141
result = idx._nat_new(box=False)
21042142
exp = np.array([tslib.iNaT] * 5, dtype=np.int64)
21052143
tm.assert_numpy_array_equal(result, exp)
2144+
2145+
def test_shift(self):
2146+
# GH 9903
2147+
idx = pd.PeriodIndex([], name='xxx', freq='H')
2148+
2149+
with tm.assertRaises(TypeError):
2150+
# period shift doesn't accept freq
2151+
idx.shift(1, freq='H')
2152+
2153+
tm.assert_index_equal(idx.shift(0), idx)
2154+
tm.assert_index_equal(idx.shift(3), idx)
2155+
2156+
idx = pd.PeriodIndex(['2011-01-01 10:00', '2011-01-01 11:00'
2157+
'2011-01-01 12:00'], name='xxx', freq='H')
2158+
tm.assert_index_equal(idx.shift(0), idx)
2159+
exp = pd.PeriodIndex(['2011-01-01 13:00', '2011-01-01 14:00'
2160+
'2011-01-01 15:00'], name='xxx', freq='H')
2161+
tm.assert_index_equal(idx.shift(3), exp)
2162+
exp = pd.PeriodIndex(['2011-01-01 07:00', '2011-01-01 08:00'
2163+
'2011-01-01 09:00'], name='xxx', freq='H')
2164+
tm.assert_index_equal(idx.shift(-3), exp)

0 commit comments

Comments
 (0)