From b09e794f8323051d8fdb5ed2a6bae25e92475665 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Wed, 8 May 2024 18:54:05 -0500 Subject: [PATCH] Add proxy for inplace operations in `cudf.pandas` (#15695) Fixes: #15676 This PR implements `__iadd__` and `__isub__` methods to allow in-place subtraction and addition operations. Forks out from #14534 Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Bradley Dice (https://github.com/bdice) URL: https://github.com/rapidsai/cudf/pull/15695 --- python/cudf/cudf/pandas/fast_slow_proxy.py | 14 +++++ .../cudf_pandas_tests/test_cudf_pandas.py | 56 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/python/cudf/cudf/pandas/fast_slow_proxy.py b/python/cudf/cudf/pandas/fast_slow_proxy.py index c66458077fa..f91cdeac149 100644 --- a/python/cudf/cudf/pandas/fast_slow_proxy.py +++ b/python/cudf/cudf/pandas/fast_slow_proxy.py @@ -1083,9 +1083,23 @@ def _replace_closurevars( # Added on a per-proxy basis # https://github.com/rapidsai/xdf/pull/306#pullrequestreview-1636155428 # "__hash__", + "__iadd__", + "__iand__", + "__iconcat__", + "__ifloordiv__", + "__ilshift__", + "__imatmul__", + "__imod__", + "__imul__", "__int__", "__invert__", + "__ior__", + "__ipow__", + "__irshift__", + "__isub__", "__iter__", + "__itruediv__", + "__ixor__", "__le__", "__len__", "__lshift__", diff --git a/python/cudf/cudf_pandas_tests/test_cudf_pandas.py b/python/cudf/cudf_pandas_tests/test_cudf_pandas.py index aa937d3ed4f..dcba1edd5fe 100644 --- a/python/cudf/cudf_pandas_tests/test_cudf_pandas.py +++ b/python/cudf/cudf_pandas_tests/test_cudf_pandas.py @@ -1243,6 +1243,62 @@ def my_apply(df, unused): tm.assert_series_equal(result, expected) +@pytest.mark.parametrize( + "op", + [ + "__iadd__", + "__iand__", + "__ifloordiv__", + "__imod__", + "__imul__", + "__ior__", + "__ipow__", + "__isub__", + "__itruediv__", + "__ixor__", + ], +) +def test_inplace_ops(op): + xdf1 = xpd.DataFrame({"a": [10, 11, 12]}) + xdf2 = xpd.DataFrame({"a": [1, 2, 3]}) + + df1 = pd.DataFrame({"a": [10, 11, 12]}) + df2 = pd.DataFrame({"a": [1, 2, 3]}) + + actual = getattr(xdf1, op)(xdf2) + expected = getattr(df1, op)(df2) + + tm.assert_equal(actual, expected) + + +@pytest.mark.parametrize( + "op", + [ + "__iadd__", + "__iand__", + "__ifloordiv__", + "__imod__", + "__imul__", + "__ior__", + "__ipow__", + "__isub__", + "__itruediv__", + "__ixor__", + ], +) +def test_inplace_ops_series(op): + xser1 = xpd.Series([10, 11, 12]) + xser2 = xpd.Series([1, 2, 3]) + + ser1 = pd.Series([10, 11, 12]) + ser2 = pd.Series([1, 2, 3]) + + actual = getattr(xser1, op)(xser2) + expected = getattr(ser1, op)(ser2) + + tm.assert_equal(actual, expected) + + @pytest.mark.parametrize("data", [pd.NaT, 1234, "nat"]) def test_timestamp(data): xtimestamp = xpd.Timestamp(data)