Skip to content

BUG: Non-unique indexing via loc and friends fixed when slicing (GH3659_) #3661

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 2 commits into from
May 20, 2013

Conversation

jreback
Copy link
Contributor

@jreback jreback commented May 20, 2013

closes #3659

This is if you try a non_monotonic selection on a non_unique index (a mouthful)!
The reason is we cannot determinate a proper start/end point on what to include

In [11]: df = DataFrame({'A' : [1,2,3,4,5,6], 'B' : [3,4,5,6,7,8]}, 
           index = [0,1,0,1,2,3])

In [18]: df
Out[18]: 
   A  B
0  1  3
1  2  4
0  3  5
1  4  6
2  5  7
3  6  8

In [12]: df.loc[1:]
KeyError: 'cannot perform a slice operation on a non-unique non-monotonic index'

On a non_unique, but monotonic index, however, slicing works normally
(notice, since we are using loc, that both endpoints ARE included)

In [13]: df = DataFrame({'A' : [1,2,3,4,5,6], 'B' : [3,4,5,6,7,8]}, 
       index = [0,1,0,1,2,3]).sort(axis=0)

In [14]: df
Out[14]: 
   A  B
0  1  3
0  3  5
1  2  4
1  4  6
2  5  7
3  6  8

In [15]: df.loc[1:]
Out[15]: 
   A  B
1  2  4
1  4  6
2  5  7
3  6  8

In [16]: df.loc[1:2]
Out[16]: 
   A  B
1  2  4
1  4  6
2  5  7

@jreback
Copy link
Contributor Author

jreback commented May 20, 2013

@wesm pls give this a once over

jreback added 2 commits May 20, 2013 17:27
BUG: deal with non_monotonic indices

CLN: convert slice_locs arrays to sliced ranges if possible
     time this happens is when a boolean array comes back from get_loc,
     means the index is non_monotonic, which is an exception in any event
@jreback
Copy link
Contributor Author

jreback commented May 20, 2013

I was making this more complicated that it was. Solution is sort of trivial (just needed to raise an exception when get_loc returns a boolean array rather than a slice, indicating index is non-monotonic and hence not slice indexable)

that is a mouthful

jreback added a commit that referenced this pull request May 20, 2013
BUG: Non-unique indexing via loc and friends fixed when slicing (GH3659_)
@jreback jreback merged commit 89ada20 into pandas-dev:master May 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: non unique indexing via loc broken
1 participant