Skip to content

Commit

Permalink
API: change datetimelike Index to raise IndexError instead ValueError (
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored and jreback committed Dec 1, 2017
1 parent 5cd5e3b commit 1eedcf6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
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):
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

0 comments on commit 1eedcf6

Please sign in to comment.