Skip to content

BUG: MultiIndex.get_indexer with method not raising for non-monotonic #55352

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 1 commit into from
Oct 2, 2023
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
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