From 9473a2cf895919542e7cb325f486b04f323fc663 Mon Sep 17 00:00:00 2001 From: David John Gagne Date: Thu, 20 Aug 2015 17:57:47 -0500 Subject: [PATCH 1/4] Fixed import error in index.py and added exception raising for value checking in the case of a mixed-integer index. Added a test to verify in test_index --- pandas/core/index.py | 6 ++++-- pandas/tests/test_index.py | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/core/index.py b/pandas/core/index.py index 7b5a6b199bc1b..8b4ea83f53b68 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -971,11 +971,13 @@ def _convert_list_indexer(self, keyarr, kind=None): if self.inferred_type == 'mixed-integer': indexer = self.get_indexer(keyarr) + if not np.all(np.in1d(indexer, self.values)): + raise IndexError("At least one item not found in index.") if (indexer >= 0).all(): return indexer - from pandas.core.indexing import _maybe_convert_indices - return _maybe_convert_indices(indexer, len(self)) + from pandas.core.indexing import maybe_convert_indices + return maybe_convert_indices(indexer, len(self)) elif not self.inferred_type == 'integer': return keyarr diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 35de75c6cdf02..e42c08d5a0d50 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1723,6 +1723,10 @@ def test_equals_op_multiindex(self): df.index == index_a tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False])) + def test_multitype_list_index_access(self): + df = pd.DataFrame(np.random.random((10, 5)), columns=["a"] + [20, 21, 22, 23]) + with self.assertRaises(IndexError): + vals = df[[22, 26, -8]] class TestCategoricalIndex(Base, tm.TestCase): _holder = CategoricalIndex From 169daa94856504a0a668ea6cdd72bee342123c18 Mon Sep 17 00:00:00 2001 From: David John Gagne Date: Thu, 20 Aug 2015 17:57:47 -0500 Subject: [PATCH 2/4] Fixed import error in index.py and added exception raising for value checking in the case of a mixed-integer index. Added a test to verify in test_index --- pandas/core/index.py | 6 ++++-- pandas/tests/test_index.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/core/index.py b/pandas/core/index.py index 7b5a6b199bc1b..8b4ea83f53b68 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -971,11 +971,13 @@ def _convert_list_indexer(self, keyarr, kind=None): if self.inferred_type == 'mixed-integer': indexer = self.get_indexer(keyarr) + if not np.all(np.in1d(indexer, self.values)): + raise IndexError("At least one item not found in index.") if (indexer >= 0).all(): return indexer - from pandas.core.indexing import _maybe_convert_indices - return _maybe_convert_indices(indexer, len(self)) + from pandas.core.indexing import maybe_convert_indices + return maybe_convert_indices(indexer, len(self)) elif not self.inferred_type == 'integer': return keyarr diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 35de75c6cdf02..ed39087971d7f 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1723,6 +1723,11 @@ def test_equals_op_multiindex(self): df.index == index_a tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False])) + def test_multitype_list_index_access(self): + df = pd.DataFrame(np.random.random((10, 5)), columns=["a"] + [20, 21, 22, 23]) + with self.assertRaises(IndexError): + vals = df[[22, 26, -8]] + self.assertEqual(df[21].shape[0], df.shape[0]) class TestCategoricalIndex(Base, tm.TestCase): _holder = CategoricalIndex From 81794cc2efd967b13496766aca4c7c5e8b69eab2 Mon Sep 17 00:00:00 2001 From: David John Gagne Date: Thu, 20 Aug 2015 17:57:47 -0500 Subject: [PATCH 3/4] BUG: GH10610 Fixed import error in index.py and added exception, test --- pandas/core/index.py | 6 ++++-- pandas/tests/test_index.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/core/index.py b/pandas/core/index.py index 7b5a6b199bc1b..8b4ea83f53b68 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -971,11 +971,13 @@ def _convert_list_indexer(self, keyarr, kind=None): if self.inferred_type == 'mixed-integer': indexer = self.get_indexer(keyarr) + if not np.all(np.in1d(indexer, self.values)): + raise IndexError("At least one item not found in index.") if (indexer >= 0).all(): return indexer - from pandas.core.indexing import _maybe_convert_indices - return _maybe_convert_indices(indexer, len(self)) + from pandas.core.indexing import maybe_convert_indices + return maybe_convert_indices(indexer, len(self)) elif not self.inferred_type == 'integer': return keyarr diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 35de75c6cdf02..ed39087971d7f 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1723,6 +1723,11 @@ def test_equals_op_multiindex(self): df.index == index_a tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False])) + def test_multitype_list_index_access(self): + df = pd.DataFrame(np.random.random((10, 5)), columns=["a"] + [20, 21, 22, 23]) + with self.assertRaises(IndexError): + vals = df[[22, 26, -8]] + self.assertEqual(df[21].shape[0], df.shape[0]) class TestCategoricalIndex(Base, tm.TestCase): _holder = CategoricalIndex From a153330501e610b650fcad4ed21e69445e111b97 Mon Sep 17 00:00:00 2001 From: David John Gagne Date: Mon, 24 Aug 2015 09:11:01 -0500 Subject: [PATCH 4/4] BUG: #10610 Altered index checking to use existing checks to raise IndexError. --- pandas/core/index.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/index.py b/pandas/core/index.py index 8b4ea83f53b68..0128f642e947f 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -971,11 +971,12 @@ def _convert_list_indexer(self, keyarr, kind=None): if self.inferred_type == 'mixed-integer': indexer = self.get_indexer(keyarr) - if not np.all(np.in1d(indexer, self.values)): - raise IndexError("At least one item not found in index.") if (indexer >= 0).all(): return indexer - + # missing values are flagged as -1 by get_indexer and negative indices are already + # converted to positive indices in the above if-statement, so the negative flags are changed to + # values outside the range of indices so as to trigger an IndexError in maybe_convert_indices + indexer[indexer < 0] = len(self) from pandas.core.indexing import maybe_convert_indices return maybe_convert_indices(indexer, len(self))