Skip to content

Commit 12df3ea

Browse files
David John Gagnejreback
David John Gagne
authored andcommitted
BUG: Fixed import error in index.py when using a mixed-integer indexing, #10610
1 parent e68bfd0 commit 12df3ea

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v0.17.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ Bug Fixes
741741
- Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`)
742742
- Bug in ``BinGrouper.group_info`` where returned values are not compatible with base class (:issue:`10914`)
743743
- Bug in clearing the cache on ``DataFrame.pop`` and a subsequent inplace op (:issue:`10912`)
744-
744+
- Bug in indexing with a mixed-integer ``Index`` causing an ``ImportError`` (:issue:`10610`)
745745
- Bug causing ``DataFrame.where`` to not respect the ``axis`` parameter when the frame has a symmetric shape. (:issue:`9736`)
746746

747747
- Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`)

pandas/core/index.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,12 @@ def _convert_list_indexer(self, keyarr, kind=None):
973973
indexer = self.get_indexer(keyarr)
974974
if (indexer >= 0).all():
975975
return indexer
976-
977-
from pandas.core.indexing import _maybe_convert_indices
978-
return _maybe_convert_indices(indexer, len(self))
976+
# missing values are flagged as -1 by get_indexer and negative indices are already
977+
# converted to positive indices in the above if-statement, so the negative flags are changed to
978+
# values outside the range of indices so as to trigger an IndexError in maybe_convert_indices
979+
indexer[indexer < 0] = len(self)
980+
from pandas.core.indexing import maybe_convert_indices
981+
return maybe_convert_indices(indexer, len(self))
979982

980983
elif not self.inferred_type == 'integer':
981984
return keyarr

pandas/tests/test_indexing.py

+6
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,12 @@ def test_indexing_mixed_frame_bug(self):
25352535

25362536
#if I look at df, then element [0,2] equals '_'. If instead I type df.ix[idx,'test'], I get '-----', finally by typing df.iloc[0,2] I get '_'.
25372537

2538+
def test_multitype_list_index_access(self):
2539+
#GH 10610
2540+
df = pd.DataFrame(np.random.random((10, 5)), columns=["a"] + [20, 21, 22, 23])
2541+
with self.assertRaises(IndexError):
2542+
vals = df[[22, 26, -8]]
2543+
self.assertEqual(df[21].shape[0], df.shape[0])
25382544

25392545
def test_set_index_nan(self):
25402546

0 commit comments

Comments
 (0)