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

Conversation

Licht-T
Copy link
Contributor

@Licht-T Licht-T commented Dec 9, 2017

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

whatsnew 0.22

@@ -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

@@ -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

@@ -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)

@@ -239,6 +239,15 @@ def test_iloc_getitem_multiindex(self):
xp = mi_labels.ix['j'].ix[:, 'j'].ix[0, 0]
assert rs == xp

# GH8856
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

@Licht-T
Copy link
Contributor Author

Licht-T commented Dec 9, 2017

@jreback Oops, sorry! I overlooked another test.
The MultiIndex loc slicing work at the first level.
https://github.com/pandas-dev/pandas/blob/master/pandas/tests/test_multilevel.py#L1829

Input:

s = pd.Series(np.arange(8), pd.MultiIndex.from_product(([0, 1, 2, 3], list('ab'))))
s.loc[::2] = 200
s

Output:

0  a    200
   b    200
1  a      2
   b      3
2  a    200
   b    200
3  a      6
   b      7

But, this returns the crazy result.

Input:

index = pd.MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two','three']],
                      labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
                      names=['first', 'second'])
frame = pd.DataFrame(np.random.randn(10, 3), index=index, columns=pd.Index(['A', 'B', 'C'], name='exp'))
frame.loc['foo':'bar':4]

Output:

exp                  A         B         C
first second
foo   one     0.870785  1.900457 -0.907938
bar   two    -0.290714 -0.211697 -0.957994

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Dec 9, 2017
@jreback
Copy link
Contributor

jreback commented Dec 9, 2017

@toobaz if you'd have a look

@Licht-T
Copy link
Contributor Author

Licht-T commented Dec 9, 2017

I think slicing by string is not good since slice seems not supporting to handle string.

@toobaz
Copy link
Member

toobaz commented Dec 12, 2017

@Licht-T : I'm confused. Unless I'm missing anything, your changes to code are limited to pd.MultiIndex.get_loc, but in up-to-date master,

... ?

@jreback
Copy link
Contributor

jreback commented Feb 10, 2018

closing as stale. ping if you want to update.

@jreback jreback closed this Feb 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: loc.__setitem__ over MultiIndex with slice key and step != 1 does strange things
3 participants