Skip to content

Commit

Permalink
Align MultiIndex.get_indexder with pandas 2.2 change (#15059)
Browse files Browse the repository at this point in the history
Aligns with pandas-dev/pandas#55352

Additionally, refactored a `pandas.PeriodIndex` usage to a non-deprecated version

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #15059
  • Loading branch information
mroeschke committed Feb 21, 2024
1 parent e7a7e48 commit fab911a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
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
47 changes: 30 additions & 17 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,21 +2076,42 @@ 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),),
reason="https://github.com/pandas-dev/pandas/issues/53452",
)
)

expected = pi.get_indexer(key, method=method)
got = gi.get_indexer(key, method=method)

assert_eq(expected, got)


@pytest.mark.xfail(
not PANDAS_GE_220, reason="Remove after pandas-2.2+ upgrade"
)
@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 +3107,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

0 comments on commit fab911a

Please sign in to comment.