Skip to content
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

Align MultiIndex.get_indexder with pandas 2.2 change #15059

Merged
merged 11 commits into from
Feb 21, 2024
6 changes: 6 additions & 0 deletions python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,12 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
raise NotImplementedError(
f"{method=} is not supported yet for MultiIndex."
)
if method in {"ffill", "bfill", "pad", "backfill"} and not (
self.is_monotonic_increasing or self.is_monotonic_decreasing
):
raise ValueError(
"index must be monotonic increasing or decreasing"
)

result = cudf.core.column.full(
len(target),
Expand Down
41 changes: 29 additions & 12 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2067,14 +2067,6 @@ def test_get_loc_multi_numeric_deviate(idx, key, result):
assert_eq(expected, got)


@pytest.mark.parametrize(
"idx",
[
pd.MultiIndex.from_tuples(
[(2, 1, 1), (1, 2, 3), (1, 2, 1), (1, 1, 10), (1, 1, 1), (2, 2, 1)]
)
],
)
@pytest.mark.parametrize(
"key",
[
Expand All @@ -2084,12 +2076,16 @@ def test_get_loc_multi_numeric_deviate(idx, key, result):
],
)
@pytest.mark.parametrize("method", [None, "ffill", "bfill"])
def test_get_indexer_multi_numeric_deviate(request, idx, key, method):
pi = idx
def test_get_indexer_multi_numeric_deviate(key, method):
pi = pd.MultiIndex.from_tuples(
[(2, 1, 1), (1, 2, 3), (1, 2, 1), (1, 1, 10), (1, 1, 1), (2, 2, 1)]
).sort_values()
gi = cudf.from_pandas(pi)
request.applymarker(
pytest.mark.xfail(
condition=method is not None and key == ((1, 2, 3),),
condition=not PANDAS_GE_220
and method is not None
and key == ((1, 2, 3),),
reason="https://github.com/pandas-dev/pandas/issues/53452",
)
)
Expand All @@ -2099,6 +2095,27 @@ def test_get_indexer_multi_numeric_deviate(request, idx, key, method):
assert_eq(expected, got)


@pytest.mark.parametrize("method", ["ffill", "bfill"])
def test_get_indexer_multi_error(method):
pi = pd.MultiIndex.from_tuples(
[(2, 1, 1), (1, 2, 3), (1, 2, 1), (1, 1, 10), (1, 1, 1), (2, 2, 1)]
)
gi = cudf.from_pandas(pi)

assert_exceptions_equal(
pi.get_indexer,
gi.get_indexer,
lfunc_args_and_kwargs=(
[],
{"target": ((1, 2, 3),), "method": method},
),
rfunc_args_and_kwargs=(
[],
{"target": ((1, 2, 3),), "method": method},
),
)


@pytest.mark.parametrize(
"idx",
[
Expand Down Expand Up @@ -3094,7 +3111,7 @@ def test_index_with_index_dtype(data, dtype):


def test_period_index_error():
pidx = pd.PeriodIndex(year=[2000, 2002], quarter=[1, 3])
pidx = pd.PeriodIndex(data=[pd.Period("2020-01")])
with pytest.raises(NotImplementedError):
cudf.from_pandas(pidx)
with pytest.raises(NotImplementedError):
Expand Down