Skip to content

BUG: iloc fails with non lex-sorted MultiIndex #13797 #14038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,9 @@ def _getitem_lowerdim(self, tup):

# we maybe be using a tuple to represent multiple dimensions here
ax0 = self.obj._get_axis(0)
if isinstance(ax0, MultiIndex):
# ...but iloc should handle the tuple as simple integer-location
# instead of checking it as multiindex representation (GH 13797)
if isinstance(ax0, MultiIndex) and self.name != 'iloc':
result = self._handle_lowerdim_multi_index_axis0(tup)
if result is not None:
return result
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/test_multilevel.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2420,6 +2420,34 @@ def test_repeat(self):
m_df = pd.Series(data, index=m_idx)
assert m_df.repeat(3).shape == (3 * len(data), )

def test_iloc_mi(self):
# GH 13797
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an informative comment on what you are testing here

# Test if iloc can handle integer locations in MultiIndexed DataFrame

data = [
['str00', 'str01'],
['str10', 'str11'],
['str20', 'srt21'],
['str30', 'str31'],
['str40', 'str41']
]

mi = pd.MultiIndex.from_tuples(
[('CC', 'A'),
('CC', 'B'),
('CC', 'B'),
('BB', 'a'),
('BB', 'b')
])

ans = pd.DataFrame(data)
df_mi = pd.DataFrame(data, index=mi)

res = pd.DataFrame([[df_mi.iloc[r, c] for c in range(2)]
for r in range(5)])

assert_frame_equal(res, ans)


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