-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: allow the iloc indexer to run off the end and not raise IndexError (GH6296) #6299
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
Conversation
API: allow the iloc indexer to run off the end and not raise IndexError (GH6296)
I've hit an issue with code from this ticket when debugging #6370 and I might have an objection. I agree that slices should behave as they do outside pandas, i.e. those that go outside container indices should be silently bounded, i.e. something along the lines of (UPD: fixed the code a bit) start, stop, step = s.start, s.stop, s.step
length = len(obj)
if start < 0:
start = max(length - start, 0)
elif start > length:
start = length
if stop < 0:
stop = max(length - stop, 0)
elif stop > length:
stop = length (there's actually a The point is that silently dropping invalid integer indexers, as in I've read that this ticket helped with #6301, is there a way to leave only slice bounding and drop integer index bounding without causing a regression there? |
Ok, I've fixed the error in my code, but the question remains for the sake of API harmonization. |
what does numpy / python do with an out of bounds indexer in a list? FYI loc does raise a KeyError if u try something like this ( with the index labels of course) |
In [1]: np.arange(100)[[101]]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-1-16b50cf7e847> in <module>()
----> 1 np.arange(100)[[101]]
IndexError: index 101 is out of bounds for size 100
This makes perfect sense, since it's not the indexing operation that fails but rather the index lookup. |
@immerrr I think you are right, a |
Awesome, thanks! |
closes #6296
Here's the behavior
I left it raising an
IndexError
if a single indexer is given that is out-of-bounds (consistent with the wayix/loc
would work)