Skip to content

Commit fb25cca

Browse files
ygrikujreback
authored andcommitted
BUG: iloc fails with non lex-sorted MultiIndex #13797
closes #13797 Author: YG-Riku <[email protected]> Closes #14038 from YG-Riku/mi-iloc-fix and squashes the following commits: 1cf86a1 [YG-Riku] BUG: iloc fails with non lex-sorted MultiIndex #13797 a23c736 [YG-Riku] BUG: iloc fails with non lex-sorted MultiIndex #13797 072d2a3 [YG-Riku] BUG: iloc fails with non lex-sorted MultiIndex #13797
1 parent 48ad2f4 commit fb25cca

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ Bug Fixes
15141514
- Bug in ``.set_index`` raises ``AmbiguousTimeError`` if new index contains DST boundary and multi levels (:issue:`12920`)
15151515
- Bug in ``.shift`` raises ``AmbiguousTimeError`` if data contains datetime near DST boundary (:issue:`13926`)
15161516
- Bug in ``pd.read_hdf()`` returns incorrect result when a ``DataFrame`` with a ``categorical`` column and a query which doesn't match any values (:issue:`13792`)
1517+
- Bug in ``.iloc`` when indexing with a non lex-sorted MultiIndex (:issue:`13797`)
15171518

15181519

15191520
- Bug in ``Series`` comparison operators when dealing with zero dim NumPy arrays (:issue:`13006`)

pandas/core/indexing.py

100644100755
+3-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,9 @@ 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+
# ...but iloc should handle the tuple as simple integer-location
907+
# instead of checking it as multiindex representation (GH 13797)
908+
if isinstance(ax0, MultiIndex) and self.name != 'iloc':
907909
result = self._handle_lowerdim_multi_index_axis0(tup)
908910
if result is not None:
909911
return result

pandas/tests/test_multilevel.py

100644100755
+28
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,34 @@ 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+
# Test if iloc can handle integer locations in MultiIndexed DataFrame
2426+
2427+
data = [
2428+
['str00', 'str01'],
2429+
['str10', 'str11'],
2430+
['str20', 'srt21'],
2431+
['str30', 'str31'],
2432+
['str40', 'str41']
2433+
]
2434+
2435+
mi = pd.MultiIndex.from_tuples(
2436+
[('CC', 'A'),
2437+
('CC', 'B'),
2438+
('CC', 'B'),
2439+
('BB', 'a'),
2440+
('BB', 'b')
2441+
])
2442+
2443+
expected = pd.DataFrame(data)
2444+
df_mi = pd.DataFrame(data, index=mi)
2445+
2446+
result = pd.DataFrame([[df_mi.iloc[r, c] for c in range(2)]
2447+
for r in range(5)])
2448+
2449+
assert_frame_equal(result, expected)
2450+
24232451

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

0 commit comments

Comments
 (0)