Skip to content

ENH: Extend UnsortedIndexError to regular indices #41571

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

Open
geophpherie opened this issue May 19, 2021 · 0 comments
Open

ENH: Extend UnsortedIndexError to regular indices #41571

geophpherie opened this issue May 19, 2021 · 0 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas

Comments

@geophpherie
Copy link

Is your feature request related to a problem?

When using .loc to index into a dataframe whose index is not sorted, a KeyError is raised that indicates the key does not exist. But sorting the index allows the same .loc command to pull a range of values from the dataframe.

Describe the solution you'd like

I'd like to see this extended to regular indices. So that when I try to index into an unsorted dataframe, the error is more informative in how to fix it.

API breaking implications

Since UnsortedIndexError is already implemented, and inherits from KeyError I don't see any breaking implications?

Describe alternatives you've considered

Seems that bringing the UnsortedIndexError to regular indices (as it is already raised for a Multi-Index) is the best idea?

Additional context

I tested this with 1.2.4

>>> df = pd.DataFrame({'val': ['a', 'b', 'c','d']}, index=[1.0, 3.0, 2.0, 4.0])
>>> df.loc[1.5:3.5]
Traceback (most recent call last):
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 975, in pandas._libs.hashtable.Float64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 982, in pandas._libs.hashtable.Float64HashTable.get_item
KeyError: 1.5

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexing.py", line 895, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexing.py", line 1102, in _getitem_axis
    return self._get_slice_axis(key, axis=axis)
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexing.py", line 1136, in _get_slice_axis
    indexer = labels.slice_indexer(
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 5277, in slice_indexer
    start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind)
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 5476, in slice_locs
    start_slice = self.get_slice_bound(start, "left", kind)
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 5396, in get_slice_bound
    raise err
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 5390, in get_slice_bound
    slc = self.get_loc(label)
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/numeric.py", line 395, in get_loc
    return super().get_loc(key, method=method, tolerance=tolerance)
  File "/Users/JBEYER/opt/miniconda3/envs/env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 1.5
>>> df.sort_index().loc[1.5:3.5]
    val
2.0   c
3.0   b
@geophpherie geophpherie added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 19, 2021
@mroeschke mroeschke added Error Reporting Incorrect or improved errors from pandas and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

No branches or pull requests

2 participants