Skip to content

Commit 28456d1

Browse files
committed
Merge pull request #7202 from jreback/iloc
BUG: Bug in multi-axis indexing with > 2 ndim and a multi-index (GH7199)
2 parents 2e1b092 + 8348414 commit 28456d1

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ Bug Fixes
535535
- Bug in recognizing out-of-bounds positional list indexers with ``iloc`` and a multi-axis tuple indexer (:issue:`7189`)
536536
- Bug in setitem with a single value, multi-index and integer indices (:issue:`7190`)
537537
- Bug in expressions evaluation with reversed ops, showing in series-dataframe ops (:issue:`7198`, :issue:`7192`)
538+
- Bug in multi-axis indexing with > 2 ndim and a multi-index (:issue:`7199`)
538539

539540
pandas 0.13.1
540541
-------------

pandas/core/indexing.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,13 @@ def _getitem_nested_tuple(self, tup):
821821
axis += 1
822822
continue
823823

824+
current_ndim = obj.ndim
824825
obj = getattr(obj, self.name)._getitem_axis(key, axis=axis, validate_iterable=True)
825826
axis += 1
826827

827-
if obj.ndim < self.ndim:
828+
# has the dim of the obj changed?
829+
# GH 7199
830+
if obj.ndim < current_ndim:
828831
axis -= 1
829832

830833
return obj

pandas/tests/test_indexing.py

+32
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,38 @@ def f():
10991099
p.iloc[0,[True,True,True],[2]]
11001100
self.assertRaises(IndexError, f)
11011101

1102+
# GH 7199
1103+
# Panel with multi-index
1104+
multi_index = pd.MultiIndex.from_tuples([('ONE', 'one'),
1105+
('TWO', 'two'),
1106+
('THREE', 'three')],
1107+
names=['UPPER', 'lower'])
1108+
1109+
simple_index = [x[0] for x in multi_index]
1110+
wd1 = Panel(items=['First', 'Second'],
1111+
major_axis=['a', 'b', 'c', 'd'],
1112+
minor_axis=multi_index)
1113+
1114+
wd2 = Panel(items=['First', 'Second'],
1115+
major_axis=['a', 'b', 'c', 'd'],
1116+
minor_axis=simple_index)
1117+
1118+
expected1 = wd1['First'].iloc[[True, True, True, False], [0, 2]]
1119+
result1 = wd1.iloc[0, [True, True, True, False], [0, 2]] # WRONG
1120+
assert_frame_equal(result1,expected1)
1121+
1122+
expected2 = wd2['First'].iloc[[True, True, True, False], [0, 2]]
1123+
result2 = wd2.iloc[0, [True, True, True, False], [0, 2]]
1124+
assert_frame_equal(result2,expected2)
1125+
1126+
expected1 = DataFrame(index=['a'],columns=multi_index,dtype='float64')
1127+
result1 = wd1.iloc[0,[0],[0,1,2]]
1128+
assert_frame_equal(result1,expected1)
1129+
1130+
expected2 = DataFrame(index=['a'],columns=simple_index,dtype='float64')
1131+
result2 = wd2.iloc[0,[0],[0,1,2]]
1132+
assert_frame_equal(result2,expected2)
1133+
11021134
def test_iloc_getitem_doc_issue(self):
11031135

11041136
# multi axis slicing issue with single block

0 commit comments

Comments
 (0)