diff --git a/doc/source/release.rst b/doc/source/release.rst index 24811bc7b1b45..8d39db39ad4a6 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -808,6 +808,7 @@ Bug Fixes - performance improvements in ``isnull`` on larger size pandas objects - Fixed various setitem with 1d ndarray that does not have a matching length to the indexer (:issue:`5508`) + - Bug in getitem with a multi-index and ``iloc`` (:issue:`5528`) pandas 0.12.0 ------------- diff --git a/pandas/core/index.py b/pandas/core/index.py index 1f2e823833810..096aff548dc9c 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -2826,6 +2826,11 @@ def reindex(self, target, method=None, level=None, limit=None, ------- (new_index, indexer, mask) : (MultiIndex, ndarray, ndarray) """ + + # a direct takeable + if takeable: + return self.take(target), target + if level is not None: if method is not None: raise TypeError('Fill method not supported if level passed') diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index e01fd6a763ceb..86c3808781694 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -389,6 +389,37 @@ def test_iloc_getitem_slice_dups(self): assert_frame_equal(df.iloc[10:,:2],df2) assert_frame_equal(df.iloc[10:,2:],df1) + def test_iloc_getitem_multiindex(self): + + df = DataFrame(np.random.randn(3, 3), + columns=[[2,2,4],[6,8,10]], + index=[[4,4,8],[8,10,12]]) + + rs = df.iloc[2] + xp = df.irow(2) + assert_series_equal(rs, xp) + + rs = df.iloc[:,2] + xp = df.icol(2) + assert_series_equal(rs, xp) + + rs = df.iloc[2,2] + xp = df.values[2,2] + self.assert_(rs == xp) + + # for multiple items + # GH 5528 + rs = df.iloc[[0,1]] + xp = df.xs(4,drop_level=False) + assert_frame_equal(rs,xp) + + tup = zip(*[['a','a','b','b'],['x','y','x','y']]) + index = MultiIndex.from_tuples(tup) + df = DataFrame(np.random.randn(4, 4), index=index) + rs = df.iloc[[2, 3]] + xp = df.xs('b',drop_level=False) + assert_frame_equal(rs,xp) + def test_iloc_getitem_out_of_bounds(self): # out-of-bounds slice @@ -409,23 +440,6 @@ def test_iloc_setitem(self): result = df.iloc[:,2:3] assert_frame_equal(result, expected) - def test_iloc_multiindex(self): - df = DataFrame(np.random.randn(3, 3), - columns=[[2,2,4],[6,8,10]], - index=[[4,4,8],[8,10,12]]) - - rs = df.iloc[2] - xp = df.irow(2) - assert_series_equal(rs, xp) - - rs = df.iloc[:,2] - xp = df.icol(2) - assert_series_equal(rs, xp) - - rs = df.iloc[2,2] - xp = df.values[2,2] - self.assert_(rs == xp) - def test_loc_getitem_int(self): # int label @@ -704,7 +718,7 @@ def test_iloc_setitem_series(self): result = s.iloc[:4] assert_series_equal(result, expected) - def test_iloc_multiindex(self): + def test_iloc_getitem_multiindex(self): mi_labels = DataFrame(np.random.randn(4, 3), columns=[['i', 'i', 'j'], ['A', 'A', 'B']], index=[['i', 'i', 'j', 'k'], ['X', 'X', 'Y','Y']])