Skip to content

.loc should accept out-of-bounds indexers for slices #8740

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
shoyer opened this issue Nov 6, 2014 · 7 comments
Closed

.loc should accept out-of-bounds indexers for slices #8740

shoyer opened this issue Nov 6, 2014 · 7 comments

Comments

@shoyer
Copy link
Member

shoyer commented Nov 6, 2014

pandas 0.14 changed .iloc to accept out-of-bounds indexers for slices. I think we should do the same for .loc, for similar reasons of consistency and efficiency.

Note that this is already currently inconsistent for int and float indexes (not entirely sure why):

>>> pd.Series(range(5), np.arange(5)).loc[-1:]
KeyError
>>> pd.Series(range(5), np.arange(5.0)).loc[-1:]
0    0
1    1
2    2
3    3
4    4
dtype: int64
@jreback
Copy link
Contributor

jreback commented Nov 6, 2014

that last is a big I think

it doesn't make sense to accept out of bounds because by definition labels don't have bounds

maybe show an example

@hayd
Copy link
Contributor

hayd commented Nov 6, 2014

by definition labels don't have bounds

It seems to me by this argument s.loc[a:b] doesn't make sense either (since being orderable implies bounded).

I'm with @shoyer here, behaviour should be consistent with iloc.

@jreback
Copy link
Contributor

jreback commented Nov 6, 2014

no the point is what is out of bounds

I your example say I did
a:20

but 20 is not in the index - what does that mean?

and when would you then raise KeyError
essentially you are saying that you can slice and get back something or an empty list

would be really odd

@jreback
Copy link
Contributor

jreback commented Nov 6, 2014

that said if you have an example eould be best

@hayd
Copy link
Contributor

hayd commented Nov 6, 2014

Sorry I wasn't clear, in the above I meant: doesn't your argument lead to disallowing s.loc[a:b] even with a and b are in the index.

However, I don't see where the ambiguity is regardless of a and b being in the index, in python:

# s.loc[a:b]
[i, val for i, val in s.iteritems() if a <= i <= b]  # or maybe < b

# s.iloc[a:b]
list(s.iteritems())[a:b]

The problem arises if they are not orderable (i.e. but that's independent of whether they're in the index) ?

@jorisvandenbossche
Copy link
Member

I think this is a duplicate of #8613 I recently filed.

But I am also in favor allowing out-of-bounds (not included labels) indexers in slices. There could only be some ambiguity with non-orderable values, but I think in most common cases this would be very useful.

@shoyer
Copy link
Member Author

shoyer commented Nov 6, 2014

@jorisvandenbossche Oops, I think I even saw that issue a few weeks ago! Let's continue the discussion over there.

I encountered this today as part of my recent indexing deep dive for intervals (#8707).

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

No branches or pull requests

4 participants