Skip to content

Commit 072d2a3

Browse files
committed
BUG: iloc fails with non lex-sorted MultiIndex #13797
corrected how iloc handles tuple-keys for multiindex
1 parent 6c73e76 commit 072d2a3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/core/indexing.py

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ def _getitem_lowerdim(self, tup):
903903

904904
# we maybe be using a tuple to represent multiple dimensions here
905905
ax0 = self.obj._get_axis(0)
906-
if isinstance(ax0, MultiIndex):
906+
if isinstance(ax0, MultiIndex) and self.name != 'iloc':
907907
result = self._handle_lowerdim_multi_index_axis0(tup)
908908
if result is not None:
909909
return result

pandas/tests/test_multilevel.py

100644100755
+19
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,25 @@ def test_repeat(self):
24202420
m_df = pd.Series(data, index=m_idx)
24212421
assert m_df.repeat(3).shape == (3 * len(data), )
24222422

2423+
def test_iloc_mi(self):
2424+
# GH 13797
2425+
2426+
ind_nonLex = [
2427+
['CC', 'CC', 'CC', 'BB', 'BB'],
2428+
['A', 'B', 'B', 'a', 'b']
2429+
]
2430+
2431+
strCol = pd.DataFrame(
2432+
['fooA', 'fooB', 'fooC', 'fooD', 'fooE'])
2433+
2434+
dat = np.arange(1, 26).reshape(5, 5)
2435+
df = pd.concat([strCol, pd.DataFrame(dat)], axis=1)
2436+
df1 = pd.DataFrame(df.values, index=ind_nonLex)
2437+
2438+
assert df1.iloc[0, 0] == 'fooA'
2439+
assert df1.iloc[4, 0] == 'fooE'
2440+
assert df1.iloc[4, 5] == 25
2441+
24232442

24242443
if __name__ == '__main__':
24252444
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)