Skip to content
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

BUG: Unsorted and Monotonic Indexes respond differently on non-existant key(s) slicing #42331

Open
attack68 opened this issue Jul 1, 2021 · 1 comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@attack68
Copy link
Contributor

attack68 commented Jul 1, 2021

For a python list, bad-indexing can result in IndexError:

>>> ls = ["a", "b", "c"]
>>> ls[1]
"b"
>>> ls[5]
IndexError("list index out of range")

Slicing behaves differently and returns empty list with bad indexes:

>>> ls[2:5]
["c"]
>>> ls[4:5]
[]

For a monotonic Index pandas behaves in the same way either when a single key or list of keys is given compared with a slice:

>>> ser = Series([1,2,3], index=["a", "b", "c"])

# single key
>>> ser.loc["a"]
1
>>> ser.loc["!"]
KeyError("!")

# list of keys
>>> ser.loc[["a", "b"]]
Series([1, 2], index=["a", "b"])
>>> ser.loc[["!", "a"]]
KeyError("['!'] not in index")
>>> ser.loc[["!", "!!"]]
KeyError("None of [Index(['!', '!!'], dtype='object')] are in the [index]")

# key slicing
>>> ser.loc[slice("a","b")]
Series([1, 2], index=["a", "b"])
>>> ser.loc[slice("a", "!")]
Series([], dtype: int64)
>>> ser.loc[slice("!", "!!")]
Series([], dtype: int64)

For a non-monotonic (unsorted) index the behaviour is the same except for the empty return:

>>> ser = Series([1,3,2], index=["a", "c", "b"])

# key slicing
>>> ser.loc[slice("a", "b")
Series([1, 3, 2], index=["a", "c", "b"])
>>> ser.loc[slice("a", "!")]
KeyError("!")
>>> ser.loc[slice("!", "!!")]
KeyError("!")

Anyone with ideas?

@attack68 attack68 added Bug Needs Triage Issue that has not been reviewed by a pandas team member Indexing Related to indexing on series/frames, not to indexes themselves Error Reporting Incorrect or improved errors from pandas labels Jul 1, 2021
@AlexKirko
Copy link
Member

My guess would be that this is because slicing a non-monothonic index is implemented as implicitly indexing by the edges, because we assume that there is no order to the index members?

@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

3 participants