From 3bf0378c4e506c3d09506670bd19c8dc287edf52 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 20 Nov 2017 15:51:08 +0100 Subject: [PATCH 1/2] API: change datetimelike Index to raise IndexError instead ValueError --- pandas/core/indexes/datetimelike.py | 4 +++- pandas/tests/indexes/test_base.py | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 4934ccb49b844..748d0da9c0212 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -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: diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 307cda7f2d1cb..38b4e2dbca283 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -591,12 +591,19 @@ 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(self, indices): - assert exp == arr[5] + if indices.size != 100: + return + + exp = getattr(indices, '_box_func', lambda x: x)(indices._values[0]) + assert indices[0] == exp + + with pytest.raises(IndexError): + indices[101] + + with pytest.raises(IndexError): + indices['no_int'] def test_intersection(self): first = self.strIndex[:20] From 27931f6d316f7f7759d2283ce21cd56fa2840301 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 30 Nov 2017 13:35:54 +0100 Subject: [PATCH 2/2] simplify test + add whatsnew --- doc/source/whatsnew/v0.22.0.txt | 2 ++ pandas/tests/indexes/test_base.py | 8 +------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 4a27bf54de695..d43d5bec7175f 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -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 ^^^ diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index c6406dceb3951..0b782e600822a 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -623,13 +623,7 @@ def test_empty_fancy(self): # Index. pytest.raises(IndexError, idx.__getitem__, empty_farr) - def test_getitem(self, indices): - - if indices.size != 100: - return - - exp = getattr(indices, '_box_func', lambda x: x)(indices._values[0]) - assert indices[0] == exp + def test_getitem_error(self, indices): with pytest.raises(IndexError): indices[101]