Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
restore getitem needed by geopandas
  • Loading branch information
jbrockmendel committed Aug 22, 2019
commit 23d2eca61bd21118b2cf26b28a5b887a2b75e1a0
71 changes: 70 additions & 1 deletion 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
from pandas.core.indexers import is_list_like_indexer, length_of_indexer


Expand Down Expand Up @@ -110,6 +110,75 @@ def __call__(self, axis=None):
new_self.axis = axis
return new_self

# TODO: remove once geopandas no longer needs this
def __getitem__(self, key):
# Used in ix and downstream in geopandas _CoordinateIndexer
if type(key) is tuple:
# Note: we check the type exactly instead of with isinstance
# because NamedTuple is checked separately.
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
try:
values = self.obj._get_value(*key)
except (KeyError, TypeError, InvalidIndexError, AttributeError):
# 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,
# so this check can be removed once ix is removed.
# The InvalidIndexError is only catched for compatibility
# with geopandas, see
# https://github.com/pandas-dev/pandas/issues/27258
# TODO: The AttributeError is for IntervalIndex which
# incorrectly implements get_value, see
# https://github.com/pandas-dev/pandas/issues/27865
pass
else:
if is_scalar(values):
return values

return self._getitem_tuple(key)
else:
# we by definition only have the 0th axis
axis = self.axis or 0

key = com.apply_if_callable(key, self.obj)
return self._getitem_axis(key, axis=axis)

# TODO: remove once geopandas no longer needs __getitem__
def _getitem_axis(self, key, axis: int):
if is_iterator(key):
key = list(key)
self._validate_key(key, axis)

labels = self.obj._get_axis(axis)
if isinstance(key, slice):
return self._get_slice_axis(key, axis=axis)
elif is_list_like_indexer(key) and not (
isinstance(key, tuple) and isinstance(labels, MultiIndex)
):

if hasattr(key, "ndim") and key.ndim > 1:
raise ValueError("Cannot index with multidimensional key")

return self._getitem_iterable(key, axis=axis)
else:

# maybe coerce a float scalar to integer
key = labels._maybe_cast_indexer(key)

if is_integer(key):
if axis == 0 and isinstance(labels, MultiIndex):
try:
return self._get_label(key, axis=axis)
except (KeyError, TypeError):
if self.obj.index.levels[0].is_integer():
raise

# this is the fallback! (for a non-float, non-integer index)
if not labels.is_floating() and not labels.is_integer():
return self._get_loc(key, axis=axis)

return self._get_label(key, axis=axis)

def _get_label(self, label, axis: int):
if self.ndim == 1:
# for perf reasons we want to try _xs first
Expand Down