Skip to content

Commit eeaf1a1

Browse files
The error message for unsupported index on shift() could be more explicit (#38176)
* Update base.py * Update base.py * Update base.py * Update base.py * Update common.py * Update common.py * Update common.py * Update test_analytics.py * Update base.py * Update base.py * Update common.py * Update test_analytics.py * Update base.py * Update common.py * Update common.py * Update common.py
1 parent 6b21e3f commit eeaf1a1

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4735,7 +4735,10 @@ def shift(self, periods=1, freq=None):
47354735
'2012-03-01'],
47364736
dtype='datetime64[ns]', freq='MS')
47374737
"""
4738-
raise NotImplementedError(f"Not supported for type {type(self).__name__}")
4738+
raise NotImplementedError(
4739+
f"This method is only implemented for DatetimeIndex, PeriodIndex and "
4740+
f"TimedeltaIndex; Got type {type(self).__name__}"
4741+
)
47394742

47404743
def argsort(self, *args, **kwargs) -> np.ndarray:
47414744
"""

pandas/tests/indexes/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def test_shift(self):
7373

7474
# GH8083 test the base class for shift
7575
idx = self.create_index()
76-
msg = f"Not supported for type {type(idx).__name__}"
76+
msg = (
77+
f"This method is only implemented for DatetimeIndex, PeriodIndex and "
78+
f"TimedeltaIndex; Got type {type(idx).__name__}"
79+
)
7780
with pytest.raises(NotImplementedError, match=msg):
7881
idx.shift(1)
7982
with pytest.raises(NotImplementedError, match=msg):

pandas/tests/indexes/multi/test_analytics.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
def test_shift(idx):
1212

1313
# GH8083 test the base class for shift
14-
msg = "Not supported for type MultiIndex"
14+
msg = "This method is only implemented for DatetimeIndex, PeriodIndex and "
15+
"TimedeltaIndex; Got type MultiIndex"
1516
with pytest.raises(NotImplementedError, match=msg):
1617
idx.shift(1)
1718
with pytest.raises(NotImplementedError, match=msg):

0 commit comments

Comments
 (0)