From aef903c15cced79906ab8a0ccce11644f1fd6de6 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Fri, 25 Aug 2023 10:09:04 -0500 Subject: [PATCH] Add pandas compatible output to `Series.unique` (#13959) Partially addresses #8175 This PR makes changes to `Series.unique`, where a cupy array is returned to match `pd.Series.unique` where a numpy array is returned. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Lawrence Mitchell (https://github.com/wence-) URL: https://github.com/rapidsai/cudf/pull/13959 --- python/cudf/cudf/core/series.py | 2 ++ python/cudf/cudf/tests/test_series.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/python/cudf/cudf/core/series.py b/python/cudf/cudf/core/series.py index b63261ef840..30d584c2270 100644 --- a/python/cudf/cudf/core/series.py +++ b/python/cudf/cudf/core/series.py @@ -2970,6 +2970,8 @@ def unique(self): dtype: object """ res = self._column.unique() + if cudf.get_option("mode.pandas_compatible"): + return res.values return Series(res, name=self.name) @_cudf_nvtx_annotate diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index db1249213f8..51c6bb1634d 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -2254,3 +2254,12 @@ def test_series_nlargest_nsmallest_str_error(attr): assert_exceptions_equal( getattr(gs, attr), getattr(ps, attr), ([], {"n": 1}), ([], {"n": 1}) ) + + +def test_series_unique_pandas_compatibility(): + gs = cudf.Series([10, 11, 12, 11, 10]) + ps = gs.to_pandas() + with cudf.option_context("mode.pandas_compatible", True): + actual = gs.unique() + expected = ps.unique() + assert_eq(actual, expected)