Skip to content

BUG: Bug in getitem with a multi-index and iloc (GH5528) #5529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
50 changes: 32 additions & 18 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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']])
Expand Down