Skip to content

REF: share IntervalIndex._check_method with CategoricalIndex #37871

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 4 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 17 additions & 10 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,11 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
method = missing.clean_reindex_fill_method(method)
target = ibase.ensure_index(target)

check_indexing_method(self, method)

if self.is_unique and self.equals(target):
return np.arange(len(self), dtype="intp")

if method == "pad" or method == "backfill":
raise NotImplementedError(
"method='pad' and method='backfill' not "
"implemented yet for CategoricalIndex"
)
elif method == "nearest":
raise NotImplementedError(
"method='nearest' not implemented yet for CategoricalIndex"
)

# Note: we use engine.get_indexer_non_unique below because, even if
# `target` is unique, any non-category entries in it will be encoded
# as -1 by _get_codes_for_get_indexer, so `codes` may not be unique.
Expand Down Expand Up @@ -704,3 +696,18 @@ def _delegate_method(self, name: str, *args, **kwargs):
if is_scalar(res):
return res
return CategoricalIndex(res, name=self.name)


def check_indexing_method(self, method):
Copy link
Contributor

Choose a reason for hiding this comment

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

why aren't you using pandas.core.missing.clean_fill_method?

Copy link
Member Author

Choose a reason for hiding this comment

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

because thats checking that the method is valid, whereas this is raising whenever the method is non-None.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok seems an odd place to put this inside a sub-class of index, i would locate elsewhere, maybe pandas.core.indexes.api (which btw we should rename to something better)

Copy link
Member Author

Choose a reason for hiding this comment

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

its definitely odd. we could put it in ExtensionIndex and then have DatetimeLikeIndex override it

Copy link
Contributor

Choose a reason for hiding this comment

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

ok thats a bit better place

Copy link
Member Author

Choose a reason for hiding this comment

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

updated+green

We're about to simplify the tar out of CategoricalIndex indexing

"""
Raise if we have a get_indexer `method` that is not supported or valid.
"""
if method is None:
return

if method in ["bfill", "backfill", "pad", "ffill", "nearest"]:
raise NotImplementedError(
f"method {method} not yet implemented for {type(self).__name__}"
)

raise ValueError("Invalid fill method")
16 changes: 3 additions & 13 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
ensure_index,
maybe_extract_name,
)
from pandas.core.indexes.category import check_indexing_method
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
from pandas.core.indexes.extension import ExtensionIndex, inherit_names
from pandas.core.indexes.multi import MultiIndex
Expand Down Expand Up @@ -582,17 +583,6 @@ def _maybe_convert_i8(self, key):

return key_i8

def _check_method(self, method):
if method is None:
return

if method in ["bfill", "backfill", "pad", "ffill", "nearest"]:
raise NotImplementedError(
f"method {method} not yet implemented for IntervalIndex"
)

raise ValueError("Invalid fill method")

def _searchsorted_monotonic(self, label, side, exclude_label=False):
if not self.is_non_overlapping_monotonic:
raise KeyError(
Expand Down Expand Up @@ -663,7 +653,7 @@ def get_loc(
>>> index.get_loc(pd.Interval(0, 1))
0
"""
self._check_method(method)
check_indexing_method(self, method)

if not is_scalar(key):
raise InvalidIndexError(key)
Expand Down Expand Up @@ -714,7 +704,7 @@ def get_indexer(
tolerance: Optional[Any] = None,
) -> np.ndarray:

self._check_method(method)
check_indexing_method(self, method)

if self.is_overlapping:
raise InvalidIndexError(
Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/indexes/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,14 @@ def test_get_indexer(self):
r1 = idx1.get_indexer(idx2)
tm.assert_almost_equal(r1, np.array([0, 1, 2, -1], dtype=np.intp))

msg = (
"method='pad' and method='backfill' not implemented yet for "
"CategoricalIndex"
)
msg = "method pad not yet implemented for CategoricalIndex"
with pytest.raises(NotImplementedError, match=msg):
idx2.get_indexer(idx1, method="pad")
msg = "method backfill not yet implemented for CategoricalIndex"
with pytest.raises(NotImplementedError, match=msg):
idx2.get_indexer(idx1, method="backfill")

msg = "method='nearest' not implemented yet for CategoricalIndex"
msg = "method nearest not yet implemented for CategoricalIndex"
with pytest.raises(NotImplementedError, match=msg):
idx2.get_indexer(idx1, method="nearest")

Expand Down