From 83484146af3511a7ffeacca79133aceb4199d0d2 Mon Sep 17 00:00:00 2001 From: jreback Date: Wed, 21 May 2014 19:28:00 -0400 Subject: [PATCH] BUG: Bug in multi-axis indexing with > 2 ndim and a multi-index (GH7199) --- doc/source/release.rst | 1 + pandas/core/indexing.py | 5 ++++- pandas/tests/test_indexing.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 200451f14a813..25202dca34a77 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -535,6 +535,7 @@ Bug Fixes - Bug in recognizing out-of-bounds positional list indexers with ``iloc`` and a multi-axis tuple indexer (:issue:`7189`) - Bug in setitem with a single value, multi-index and integer indices (:issue:`7190`) - Bug in expressions evaluation with reversed ops, showing in series-dataframe ops (:issue:`7198`, :issue:`7192`) +- Bug in multi-axis indexing with > 2 ndim and a multi-index (:issue:`7199`) pandas 0.13.1 ------------- diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index cf8ad0f48a939..4ecb5983a186a 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -821,10 +821,13 @@ def _getitem_nested_tuple(self, tup): axis += 1 continue + current_ndim = obj.ndim obj = getattr(obj, self.name)._getitem_axis(key, axis=axis, validate_iterable=True) axis += 1 - if obj.ndim < self.ndim: + # has the dim of the obj changed? + # GH 7199 + if obj.ndim < current_ndim: axis -= 1 return obj diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index a2550810ef13d..d05f7fc711683 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -1099,6 +1099,38 @@ def f(): p.iloc[0,[True,True,True],[2]] self.assertRaises(IndexError, f) + # GH 7199 + # Panel with multi-index + multi_index = pd.MultiIndex.from_tuples([('ONE', 'one'), + ('TWO', 'two'), + ('THREE', 'three')], + names=['UPPER', 'lower']) + + simple_index = [x[0] for x in multi_index] + wd1 = Panel(items=['First', 'Second'], + major_axis=['a', 'b', 'c', 'd'], + minor_axis=multi_index) + + wd2 = Panel(items=['First', 'Second'], + major_axis=['a', 'b', 'c', 'd'], + minor_axis=simple_index) + + expected1 = wd1['First'].iloc[[True, True, True, False], [0, 2]] + result1 = wd1.iloc[0, [True, True, True, False], [0, 2]] # WRONG + assert_frame_equal(result1,expected1) + + expected2 = wd2['First'].iloc[[True, True, True, False], [0, 2]] + result2 = wd2.iloc[0, [True, True, True, False], [0, 2]] + assert_frame_equal(result2,expected2) + + expected1 = DataFrame(index=['a'],columns=multi_index,dtype='float64') + result1 = wd1.iloc[0,[0],[0,1,2]] + assert_frame_equal(result1,expected1) + + expected2 = DataFrame(index=['a'],columns=simple_index,dtype='float64') + result2 = wd2.iloc[0,[0],[0,1,2]] + assert_frame_equal(result2,expected2) + def test_iloc_getitem_doc_issue(self): # multi axis slicing issue with single block