Skip to content

Commit

Permalink
raise ValueError for get_indexer with method and non-monotonic
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemanley committed Oct 2, 2023
1 parent 0db27c0 commit 90d4abb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Missing

MultiIndex
^^^^^^^^^^
-
- Bug in :meth:`MultiIndex.get_indexer` not raising ``ValueError`` when ``method`` provided and index is non-monotonic (:issue:`53452`)
-

I/O
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4011,8 +4011,8 @@ def _get_fill_indexer(
self, target: Index, method: str_t, limit: int | None = None, tolerance=None
) -> npt.NDArray[np.intp]:
if self._is_multi:
# TODO: get_indexer_with_fill docstring says values must be _sorted_
# but that doesn't appear to be enforced
if not (self.is_monotonic_increasing or self.is_monotonic_decreasing):
raise ValueError("index must be monotonic increasing or decreasing")
# error: "IndexEngine" has no attribute "get_indexer_with_fill"
engine = self._engine
with warnings.catch_warnings():
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,19 @@ def test_get_indexer_methods(self):
expected = np.array([4, 6, 7], dtype=pad_indexer.dtype)
tm.assert_almost_equal(expected, pad_indexer)

@pytest.mark.parametrize("method", ["pad", "ffill", "backfill", "bfill", "nearest"])
def test_get_indexer_methods_raise_for_non_monotonic(self, method):
# 53452
mi = MultiIndex.from_arrays([[0, 4, 2], [0, 4, 2]])
if method == "nearest":
err = NotImplementedError
msg = "not implemented yet for MultiIndex"
else:
err = ValueError
msg = "index must be monotonic increasing or decreasing"
with pytest.raises(err, match=msg):
mi.get_indexer([(1, 1)], method=method)

def test_get_indexer_three_or_more_levels(self):
# https://github.com/pandas-dev/pandas/issues/29896
# tests get_indexer() on MultiIndexes with 3+ levels
Expand Down

0 comments on commit 90d4abb

Please sign in to comment.