Skip to content

REF: avoid accessing index._engine in set_with_engine #41959

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 3 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
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: 1 addition & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
Dtype,
DtypeObj,
)
from pandas.errors import InvalidIndexError
from pandas.util._decorators import (
cache_readonly,
doc,
Expand Down Expand Up @@ -658,8 +657,7 @@ def get_loc(self, key, method=None, tolerance=None):
-------
loc : int
"""
if not is_scalar(key):
raise InvalidIndexError(key)
self._check_indexing_error(key)

orig_key = key
if is_valid_na_for_dtype(key, self.dtype):
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,7 @@ def get_loc(
0
"""
self._check_indexing_method(method)

if not is_scalar(key):
raise InvalidIndexError(key)
self._check_indexing_error(key)

if isinstance(key, Interval):
if self.closed != key.closed:
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
Dtype,
DtypeObj,
)
from pandas.errors import InvalidIndexError
from pandas.util._decorators import doc

from pandas.core.dtypes.common import (
is_datetime64_any_dtype,
is_float,
is_integer,
is_scalar,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import PeriodDtype
Expand Down Expand Up @@ -411,9 +409,7 @@ def get_loc(self, key, method=None, tolerance=None):
"""
orig_key = key

if not is_scalar(key):
raise InvalidIndexError(key)

self._check_indexing_error(key)
if isinstance(key, str):

try:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def get_loc(self, key, method=None, tolerance=None):
return self._range.index(new_key)
except ValueError as err:
raise KeyError(key) from err
self._check_indexing_error(key)
raise KeyError(key)
return super().get_loc(key, method=method, tolerance=tolerance)

Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
DtypeObj,
Optional,
)
from pandas.errors import InvalidIndexError

from pandas.core.dtypes.common import (
TD64NS_DTYPE,
Expand Down Expand Up @@ -170,8 +169,7 @@ def get_loc(self, key, method=None, tolerance=None):
-------
loc : int, slice, or ndarray[int]
"""
if not is_scalar(key):
raise InvalidIndexError(key)
self._check_indexing_error(key)

try:
key = self._data._validate_scalar(key, unbox=False)
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ def __setitem__(self, key, value) -> None:
# GH#12862 adding a new key to the Series
self.loc[key] = value

except TypeError as err:
except (InvalidIndexError, TypeError) as err:
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
raise KeyError(
"key of type tuple not found and not a MultiIndex"
Expand All @@ -1094,8 +1094,7 @@ def __setitem__(self, key, value) -> None:
self._maybe_update_cacher()

def _set_with_engine(self, key, value) -> None:
# fails with AttributeError for IntervalIndex
loc = self.index._engine.get_loc(key)
loc = self.index.get_loc(key)
# error: Argument 1 to "validate_numeric_casting" has incompatible type
# "Union[dtype, ExtensionDtype]"; expected "dtype"
validate_numeric_casting(self.dtype, value) # type: ignore[arg-type]
Expand All @@ -1112,6 +1111,9 @@ def _set_with(self, key, value):

if is_scalar(key):
key = [key]
elif is_iterator(key):
# Without this, the call to infer_dtype will consume the generator
key = list(key)

if isinstance(key, Index):
key_type = key.inferred_type
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/indexes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
IntervalIndex,
MultiIndex,
PeriodIndex,
RangeIndex,
Series,
TimedeltaIndex,
UInt64Index,
Expand Down Expand Up @@ -181,6 +182,27 @@ def test_get_value(self, index):
tm.assert_almost_equal(result, values[67])


class TestGetLoc:
def test_get_loc_non_hashable(self, index):
# MultiIndex and Index raise TypeError, others InvalidIndexError

with pytest.raises((TypeError, InvalidIndexError), match="slice"):
index.get_loc(slice(0, 1))

def test_get_loc_generator(self, index):

exc = KeyError
if isinstance(
index,
(DatetimeIndex, TimedeltaIndex, PeriodIndex, RangeIndex, IntervalIndex),
):
# TODO: make these more consistent?
exc = InvalidIndexError
with pytest.raises(exc, match="generator object"):
# MultiIndex specifically checks for generator; others for scalar
index.get_loc(x for x in range(5))


class TestGetIndexer:
def test_get_indexer_base(self, index):

Expand Down
9 changes: 0 additions & 9 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ def test_setitem_ndarray_3d(self, index, frame_or_series, indexer_sli):
if indexer_sli is tm.iloc:
err = ValueError
msg = f"Cannot set values with ndim > {obj.ndim}"
elif (
isinstance(index, pd.IntervalIndex)
and indexer_sli is tm.setitem
and obj.ndim == 1
):
err = AttributeError
msg = (
"'pandas._libs.interval.IntervalTree' object has no attribute 'get_loc'"
)
else:
err = ValueError
msg = "|".join(
Expand Down