Skip to content

Commit 1eedcf6

Browse files
jorisvandenbosschejreback
authored andcommitted
API: change datetimelike Index to raise IndexError instead ValueError (#18386)
1 parent 5cd5e3b commit 1eedcf6

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

doc/source/whatsnew/v0.22.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ Indexing
150150
- Bug in :class:`Index`` construction from list of mixed type tuples (:issue:`18505`)
151151
- Bug in :class:`IntervalIndex` where empty and purely NA data was constructed inconsistently depending on the construction method (:issue:`18421`)
152152
- Bug in ``IntervalIndex.symmetric_difference()`` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
153+
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).
154+
153155

154156
I/O
155157
^^^

pandas/core/indexes/datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ def __getitem__(self, key):
263263

264264
is_int = is_integer(key)
265265
if is_scalar(key) and not is_int:
266-
raise ValueError
266+
raise IndexError("only integers, slices (`:`), ellipsis (`...`), "
267+
"numpy.newaxis (`None`) and integer or boolean "
268+
"arrays are valid indices")
267269

268270
getitem = self._data.__getitem__
269271
if is_int:

pandas/tests/indexes/test_base.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,13 @@ def test_empty_fancy(self):
623623
# Index.
624624
pytest.raises(IndexError, idx.__getitem__, empty_farr)
625625

626-
def test_getitem(self):
627-
arr = np.array(self.dateIndex)
628-
exp = self.dateIndex[5]
629-
exp = _to_m8(exp)
626+
def test_getitem_error(self, indices):
630627

631-
assert exp == arr[5]
628+
with pytest.raises(IndexError):
629+
indices[101]
630+
631+
with pytest.raises(IndexError):
632+
indices['no_int']
632633

633634
def test_intersection(self):
634635
first = self.strIndex[:20]

0 commit comments

Comments
 (0)