Skip to content

BUG: Fix df.loc slice support #18703

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,9 @@ def _maybe_to_slice(loc):
mask[loc] = True
return mask

if not isinstance(key, tuple):
if isinstance(key, slice):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u add some comments here

return key
elif not isinstance(key, tuple):
loc = self._get_level_indexer(key, level=0)
return _maybe_to_slice(loc)

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexing/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ def test_iloc_getitem_multiindex(self):
xp = mi_labels.ix['j'].ix[:, 'j'].ix[0, 0]
assert rs == xp

# GH8856
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make separate test

s = pd.Series(np.arange(10),
pd.MultiIndex.from_product(([0, 1], list('abcde'))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u add several cases using slices

also if other testing of slice u can pull to this section

result = s.iloc[::4]
expected = pd.Series(np.array([0, 4, 8]),
MultiIndex(levels=[[0, 1], list('ade')],
labels=[[0, 0, 1], [0, 2, 1]]))
tm.assert_series_equal(result, expected)

def test_loc_multiindex(self):

mi_labels = DataFrame(np.random.randn(3, 3),
Expand Down Expand Up @@ -278,6 +287,15 @@ def test_loc_multiindex(self):
xp = mi_int.ix[4]
tm.assert_frame_equal(rs, xp)

# GH8856
s = pd.Series(np.arange(10),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a duplicate of the other test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(.iloc vs. .loc)

pd.MultiIndex.from_product(([0, 1], list('abcde'))))
result = s.loc[::4]
expected = pd.Series(np.array([0, 4, 8]),
MultiIndex(levels=[[0, 1], ['a', 'd', 'e']],
labels=[[0, 0, 1], [0, 2, 1]]))
tm.assert_series_equal(result, expected)

def test_getitem_partial_int(self):
# GH 12416
# with single item
Expand Down