Skip to content

Commit bf7dba7

Browse files
authored
REF: share IntervalIndex._check_method with CategoricalIndex (#37871)
1 parent 1f89b82 commit bf7dba7

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

pandas/core/indexes/category.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -529,19 +529,11 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
529529
method = missing.clean_reindex_fill_method(method)
530530
target = ibase.ensure_index(target)
531531

532+
self._check_indexing_method(method)
533+
532534
if self.is_unique and self.equals(target):
533535
return np.arange(len(self), dtype="intp")
534536

535-
if method == "pad" or method == "backfill":
536-
raise NotImplementedError(
537-
"method='pad' and method='backfill' not "
538-
"implemented yet for CategoricalIndex"
539-
)
540-
elif method == "nearest":
541-
raise NotImplementedError(
542-
"method='nearest' not implemented yet for CategoricalIndex"
543-
)
544-
545537
# Note: we use engine.get_indexer_non_unique below because, even if
546538
# `target` is unique, any non-category entries in it will be encoded
547539
# as -1 by _get_codes_for_get_indexer, so `codes` may not be unique.

pandas/core/indexes/extension.py

+15
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ def __getitem__(self, key):
230230

231231
# ---------------------------------------------------------------------
232232

233+
def _check_indexing_method(self, method):
234+
"""
235+
Raise if we have a get_indexer `method` that is not supported or valid.
236+
"""
237+
# GH#37871 for now this is only for IntervalIndex and CategoricalIndex
238+
if method is None:
239+
return
240+
241+
if method in ["bfill", "backfill", "pad", "ffill", "nearest"]:
242+
raise NotImplementedError(
243+
f"method {method} not yet implemented for {type(self).__name__}"
244+
)
245+
246+
raise ValueError("Invalid fill method")
247+
233248
def _get_engine_target(self) -> np.ndarray:
234249
return np.asarray(self._data)
235250

pandas/core/indexes/interval.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,6 @@ def _maybe_convert_i8(self, key):
582582

583583
return key_i8
584584

585-
def _check_method(self, method):
586-
if method is None:
587-
return
588-
589-
if method in ["bfill", "backfill", "pad", "ffill", "nearest"]:
590-
raise NotImplementedError(
591-
f"method {method} not yet implemented for IntervalIndex"
592-
)
593-
594-
raise ValueError("Invalid fill method")
595-
596585
def _searchsorted_monotonic(self, label, side, exclude_label=False):
597586
if not self.is_non_overlapping_monotonic:
598587
raise KeyError(
@@ -663,7 +652,7 @@ def get_loc(
663652
>>> index.get_loc(pd.Interval(0, 1))
664653
0
665654
"""
666-
self._check_method(method)
655+
self._check_indexing_method(method)
667656

668657
if not is_scalar(key):
669658
raise InvalidIndexError(key)
@@ -714,7 +703,7 @@ def get_indexer(
714703
tolerance: Optional[Any] = None,
715704
) -> np.ndarray:
716705

717-
self._check_method(method)
706+
self._check_indexing_method(method)
718707

719708
if self.is_overlapping:
720709
raise InvalidIndexError(

pandas/tests/indexes/categorical/test_indexing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,14 @@ def test_get_indexer(self):
238238
r1 = idx1.get_indexer(idx2)
239239
tm.assert_almost_equal(r1, np.array([0, 1, 2, -1], dtype=np.intp))
240240

241-
msg = (
242-
"method='pad' and method='backfill' not implemented yet for "
243-
"CategoricalIndex"
244-
)
241+
msg = "method pad not yet implemented for CategoricalIndex"
245242
with pytest.raises(NotImplementedError, match=msg):
246243
idx2.get_indexer(idx1, method="pad")
244+
msg = "method backfill not yet implemented for CategoricalIndex"
247245
with pytest.raises(NotImplementedError, match=msg):
248246
idx2.get_indexer(idx1, method="backfill")
249247

250-
msg = "method='nearest' not implemented yet for CategoricalIndex"
248+
msg = "method nearest not yet implemented for CategoricalIndex"
251249
with pytest.raises(NotImplementedError, match=msg):
252250
idx2.get_indexer(idx1, method="nearest")
253251

0 commit comments

Comments
 (0)