Skip to content

API: change datetimelike Index to raise IndexError instead ValueError #18386

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
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Indexing
- Bug in :class:`Index`` construction from list of mixed type tuples (:issue:`18505`)
- Bug in :class:`IntervalIndex` where empty and purely NA data was constructed inconsistently depending on the construction method (:issue:`18421`)
- Bug in ``IntervalIndex.symmetric_difference()`` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).


I/O
^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ def __getitem__(self, key):

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

getitem = self._data.__getitem__
if is_int:
Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,13 @@ def test_empty_fancy(self):
# Index.
pytest.raises(IndexError, idx.__getitem__, empty_farr)

def test_getitem(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this original test is pretty useless, so no need to keep its function (and this is tested else in many places as well)
all of the indexes have a test_indexing.py

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just rename the test to test_getitem_error. if you want you can split this out into a new file, test_indexing.py in this dir.

arr = np.array(self.dateIndex)
exp = self.dateIndex[5]
exp = _to_m8(exp)
def test_getitem_error(self, indices):

assert exp == arr[5]
with pytest.raises(IndexError):
indices[101]

with pytest.raises(IndexError):
indices['no_int']

def test_intersection(self):
first = self.strIndex[:20]
Expand Down