From f6e2355dfefb1a02a984425aabeca7a4fcb2bfde Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Wed, 28 Aug 2024 19:03:34 -0500 Subject: [PATCH] Handle `ordered` parameter in `CategoricalIndex.__repr__` (#16683) Thanks @mroeschke for catching this in https://github.com/rapidsai/cudf/pull/16665#discussion_r1735277661 This PR factors in the `ordered` parameter while generating the `repr` for `CategoricalIndex`. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Matthew Roeschke (https://github.com/mroeschke) URL: https://github.com/rapidsai/cudf/pull/16683 --- python/cudf/cudf/core/index.py | 1 + python/cudf/cudf/tests/test_repr.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 500fc580097..fc35ffa3744 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -1456,6 +1456,7 @@ def __repr__(self): pd_preprocess.dtype._categories = ( preprocess.categories.to_pandas() ) + pd_preprocess.dtype._ordered = preprocess.dtype.ordered cats_repr = repr(pd_preprocess).split("\n") output = "\n".join(data_repr[:-1] + cats_repr[-1:]) diff --git a/python/cudf/cudf/tests/test_repr.py b/python/cudf/cudf/tests/test_repr.py index 57eef9e3463..681b467f66c 100644 --- a/python/cudf/cudf/tests/test_repr.py +++ b/python/cudf/cudf/tests/test_repr.py @@ -1491,3 +1491,11 @@ def test_large_unique_categories_repr(): with utils.cudf_timeout(2, timeout_message="Failed to repr fast enough"): actual_repr = repr(gi) assert expected_repr == actual_repr + + +@pytest.mark.parametrize("ordered", [True, False]) +def test_categorical_index_ordered(ordered): + pi = pd.CategoricalIndex(range(10), ordered=ordered) + gi = cudf.CategoricalIndex(range(10), ordered=ordered) + + assert repr(pi) == repr(gi)