From a0e662e73b8f25c80b61045764cfe320d26004e1 Mon Sep 17 00:00:00 2001 From: Prashant Anand Date: Sat, 2 May 2020 11:58:13 +0900 Subject: [PATCH 1/4] TST: raise InvalidIndexError with IntervalIndex.get_value and get_loc ensure InvalidIndexError is raise when non-scalar is passed as key to get_loc and get_value methods for IntervalIndex --- .../tests/indexes/interval/test_interval.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 1b2bfa8573c21..1272747deff7d 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -19,6 +19,7 @@ ) import pandas._testing as tm import pandas.core.common as com +from pandas.core.indexes.base import InvalidIndexError @pytest.fixture(scope="class", params=[None, "foo"]) @@ -857,6 +858,37 @@ def test_is_all_dates(self): year_2017_index = pd.IntervalIndex([year_2017]) assert not year_2017_index.is_all_dates + def test_get_loc_errors(self): + idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) + + # key is not scalar + # GH 31117 + key = [5] + msg = str(key) + with pytest.raises(InvalidIndexError, match=msg): + idx.get_loc(key) + + key = (2, 3) + msg = str(key) + with pytest.raises(InvalidIndexError, match=msg): + idx.get_loc(key) + + def test_get_value_errors(self): + idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) + s = pd.Series(range(len(idx)), index=idx) + + # key is not scalar + # GH 31117 + key = [5] + msg = str(key) + with pytest.raises(InvalidIndexError, match=msg): + idx.get_value(s, key) + + key = (2, 3) + msg = str(key) + with pytest.raises(InvalidIndexError, match=msg): + idx.get_value(s, key) + def test_dir(): # GH#27571 dir(interval_index) should not raise From 46ad6f74e0ee5d3c7829abbd1cd578b315a0f4ba Mon Sep 17 00:00:00 2001 From: Prashant Anand Date: Sat, 2 May 2020 16:49:44 +0900 Subject: [PATCH 2/4] TST: moved get_loc tests --- pandas/tests/indexes/interval/test_indexing.py | 14 ++++++++++++++ pandas/tests/indexes/interval/test_interval.py | 15 --------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pandas/tests/indexes/interval/test_indexing.py b/pandas/tests/indexes/interval/test_indexing.py index 0e08a3f41b666..65c103679d8d0 100644 --- a/pandas/tests/indexes/interval/test_indexing.py +++ b/pandas/tests/indexes/interval/test_indexing.py @@ -158,6 +158,20 @@ def test_get_loc_decreasing(self, values): expected = 0 assert result == expected + def test_get_loc_non_scalar_errors(self): + idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) + + # GH 31117 + key = [5] + msg = str(key) + with pytest.raises(InvalidIndexError, match=msg): + idx.get_loc(key) + + key = (2, 3) + msg = str(key) + with pytest.raises(InvalidIndexError, match=msg): + idx.get_loc(key) + class TestGetIndexer: @pytest.mark.parametrize( diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 1272747deff7d..a2d94d71d0d68 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -858,21 +858,6 @@ def test_is_all_dates(self): year_2017_index = pd.IntervalIndex([year_2017]) assert not year_2017_index.is_all_dates - def test_get_loc_errors(self): - idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) - - # key is not scalar - # GH 31117 - key = [5] - msg = str(key) - with pytest.raises(InvalidIndexError, match=msg): - idx.get_loc(key) - - key = (2, 3) - msg = str(key) - with pytest.raises(InvalidIndexError, match=msg): - idx.get_loc(key) - def test_get_value_errors(self): idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) s = pd.Series(range(len(idx)), index=idx) From 27e270f92a2b57bec7678dfd3e342df4244b6c03 Mon Sep 17 00:00:00 2001 From: Prashant Anand Date: Wed, 6 May 2020 08:39:57 +0900 Subject: [PATCH 3/4] TST: parametrize test_get_value_non_scalar_errors and catch FutureWarning --- pandas/tests/indexes/interval/test_interval.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index a2d94d71d0d68..3f29e6ce0beb6 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -858,21 +858,16 @@ def test_is_all_dates(self): year_2017_index = pd.IntervalIndex([year_2017]) assert not year_2017_index.is_all_dates - def test_get_value_errors(self): + @pytest.mark.parametrize("key", [[5], (2, 3)]) + def test_get_value_non_scalar_errors(self, key): + # GH 31117 idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) s = pd.Series(range(len(idx)), index=idx) - # key is not scalar - # GH 31117 - key = [5] - msg = str(key) - with pytest.raises(InvalidIndexError, match=msg): - idx.get_value(s, key) - - key = (2, 3) msg = str(key) with pytest.raises(InvalidIndexError, match=msg): - idx.get_value(s, key) + with tm.assert_produces_warning(FutureWarning): + idx.get_value(s, key) def test_dir(): From 52c8ed40fc41b15c80d0a34d475ac7436f9cba02 Mon Sep 17 00:00:00 2001 From: Prashant Anand Date: Wed, 6 May 2020 08:41:37 +0900 Subject: [PATCH 4/4] TST: parametrize test_get_loc_non_scalar_errors --- pandas/tests/indexes/interval/test_indexing.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexes/interval/test_indexing.py b/pandas/tests/indexes/interval/test_indexing.py index 65c103679d8d0..718136fca6c80 100644 --- a/pandas/tests/indexes/interval/test_indexing.py +++ b/pandas/tests/indexes/interval/test_indexing.py @@ -158,16 +158,11 @@ def test_get_loc_decreasing(self, values): expected = 0 assert result == expected - def test_get_loc_non_scalar_errors(self): - idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) - + @pytest.mark.parametrize("key", [[5], (2, 3)]) + def test_get_loc_non_scalar_errors(self, key): # GH 31117 - key = [5] - msg = str(key) - with pytest.raises(InvalidIndexError, match=msg): - idx.get_loc(key) + idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)]) - key = (2, 3) msg = str(key) with pytest.raises(InvalidIndexError, match=msg): idx.get_loc(key)