Skip to content

COMPAT: catch InvalidIndexError in base Indexer getitem #27259

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
Jul 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pandas.core.dtypes.missing import _infer_fill_value, isna

import pandas.core.common as com
from pandas.core.index import Index, MultiIndex
from pandas.core.index import Index, InvalidIndexError, MultiIndex


# the supported indexers
Expand Down Expand Up @@ -118,7 +118,7 @@ def __getitem__(self, key):
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
try:
values = self.obj._get_value(*key)
except (KeyError, TypeError):
except (KeyError, TypeError, InvalidIndexError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment about why this is caught here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to test this somehow as well?

Copy link
Member Author

@jorisvandenbossche jorisvandenbossche Jul 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #27258 for a discussion on that. I can add a test with the code snippet I show there, but is a rather dummy example ..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is fine, though why are you not using .loc and using __getitem__?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you not using .loc and using __getitem__?

This is from a custom Indexer attribute in geopandas, so it is on purpose not using .loc. Now, I don't say it is the best implementation by using such pandas internals, opened an issue about that in geopandas geopandas/geopandas#1042

# TypeError occurs here if the key has non-hashable entries,
# generally slice or list.
# TODO(ix): most/all of the TypeError cases here are for ix,
Expand Down