Skip to content

Commit 4718ffe

Browse files
committed
Merge pull request #5529 from jreback/iloc_mi
BUG: Bug in getitem with a multi-index and iloc (GH5528)
2 parents 34c0db8 + d0c7832 commit 4718ffe

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ Bug Fixes
808808
- performance improvements in ``isnull`` on larger size pandas objects
809809
- Fixed various setitem with 1d ndarray that does not have a matching
810810
length to the indexer (:issue:`5508`)
811+
- Bug in getitem with a multi-index and ``iloc`` (:issue:`5528`)
811812

812813
pandas 0.12.0
813814
-------------

pandas/core/index.py

+5
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,11 @@ def reindex(self, target, method=None, level=None, limit=None,
28262826
-------
28272827
(new_index, indexer, mask) : (MultiIndex, ndarray, ndarray)
28282828
"""
2829+
2830+
# a direct takeable
2831+
if takeable:
2832+
return self.take(target), target
2833+
28292834
if level is not None:
28302835
if method is not None:
28312836
raise TypeError('Fill method not supported if level passed')

pandas/tests/test_indexing.py

+32-18
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,37 @@ def test_iloc_getitem_slice_dups(self):
389389
assert_frame_equal(df.iloc[10:,:2],df2)
390390
assert_frame_equal(df.iloc[10:,2:],df1)
391391

392+
def test_iloc_getitem_multiindex(self):
393+
394+
df = DataFrame(np.random.randn(3, 3),
395+
columns=[[2,2,4],[6,8,10]],
396+
index=[[4,4,8],[8,10,12]])
397+
398+
rs = df.iloc[2]
399+
xp = df.irow(2)
400+
assert_series_equal(rs, xp)
401+
402+
rs = df.iloc[:,2]
403+
xp = df.icol(2)
404+
assert_series_equal(rs, xp)
405+
406+
rs = df.iloc[2,2]
407+
xp = df.values[2,2]
408+
self.assert_(rs == xp)
409+
410+
# for multiple items
411+
# GH 5528
412+
rs = df.iloc[[0,1]]
413+
xp = df.xs(4,drop_level=False)
414+
assert_frame_equal(rs,xp)
415+
416+
tup = zip(*[['a','a','b','b'],['x','y','x','y']])
417+
index = MultiIndex.from_tuples(tup)
418+
df = DataFrame(np.random.randn(4, 4), index=index)
419+
rs = df.iloc[[2, 3]]
420+
xp = df.xs('b',drop_level=False)
421+
assert_frame_equal(rs,xp)
422+
392423
def test_iloc_getitem_out_of_bounds(self):
393424

394425
# out-of-bounds slice
@@ -409,23 +440,6 @@ def test_iloc_setitem(self):
409440
result = df.iloc[:,2:3]
410441
assert_frame_equal(result, expected)
411442

412-
def test_iloc_multiindex(self):
413-
df = DataFrame(np.random.randn(3, 3),
414-
columns=[[2,2,4],[6,8,10]],
415-
index=[[4,4,8],[8,10,12]])
416-
417-
rs = df.iloc[2]
418-
xp = df.irow(2)
419-
assert_series_equal(rs, xp)
420-
421-
rs = df.iloc[:,2]
422-
xp = df.icol(2)
423-
assert_series_equal(rs, xp)
424-
425-
rs = df.iloc[2,2]
426-
xp = df.values[2,2]
427-
self.assert_(rs == xp)
428-
429443
def test_loc_getitem_int(self):
430444

431445
# int label
@@ -704,7 +718,7 @@ def test_iloc_setitem_series(self):
704718
result = s.iloc[:4]
705719
assert_series_equal(result, expected)
706720

707-
def test_iloc_multiindex(self):
721+
def test_iloc_getitem_multiindex(self):
708722
mi_labels = DataFrame(np.random.randn(4, 3), columns=[['i', 'i', 'j'],
709723
['A', 'A', 'B']],
710724
index=[['i', 'i', 'j', 'k'], ['X', 'X', 'Y','Y']])

0 commit comments

Comments
 (0)