From 65e572dc2ae8b302dd658ce66f85f476f1334775 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Mon, 14 Aug 2023 08:06:44 -0500 Subject: [PATCH] Fix return type of `MultiIndex.levels` (#13870) Fixes: #13863 This PR fixes the return type of `MultiIndex.levels` to return `Index` objects. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Lawrence Mitchell (https://github.com/wence-) URL: https://github.com/rapidsai/cudf/pull/13870 --- python/cudf/cudf/core/multiindex.py | 13 ++++--------- python/cudf/cudf/tests/test_multiindex.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/python/cudf/cudf/core/multiindex.py b/python/cudf/cudf/core/multiindex.py index bb0c25a9970..6e9d068ef50 100644 --- a/python/cudf/cudf/core/multiindex.py +++ b/python/cudf/cudf/core/multiindex.py @@ -619,14 +619,8 @@ def levels(self): (3, 12)], names=['a', 'b']) >>> midx.levels - [0 1 - 1 2 - 2 3 - dtype: int64, 0 10 - 1 11 - 2 12 - dtype: int64] - """ + [Int64Index([1, 2, 3], dtype='int64', name='a'), Int64Index([10, 11, 12], dtype='int64', name='b')] + """ # noqa: E501 if self._levels is None: self._compute_levels_and_codes() return self._levels @@ -772,8 +766,9 @@ def _compute_levels_and_codes(self): # `factorize` show up in other parts of public APIs. warnings.simplefilter("ignore") code, cats = cudf.Series._from_data({None: col}).factorize() + cats.name = name codes[name] = code.astype(np.int64) - levels.append(cudf.Series(cats, name=None)) + levels.append(cats) self._levels = levels self._codes = cudf.DataFrame._from_data(codes) diff --git a/python/cudf/cudf/tests/test_multiindex.py b/python/cudf/cudf/tests/test_multiindex.py index 30960724bd6..a4099bb7f88 100644 --- a/python/cudf/cudf/tests/test_multiindex.py +++ b/python/cudf/cudf/tests/test_multiindex.py @@ -1868,3 +1868,13 @@ def test_multiindex_index_single_row(): gdf.index = idx pdf = gdf.to_pandas() assert_eq(pdf.loc[("b", 3)], gdf.loc[("b", 3)]) + + +def test_multiindex_levels(): + gidx = cudf.MultiIndex.from_product( + [range(3), ["one", "two"]], names=["first", "second"] + ) + pidx = gidx.to_pandas() + + assert_eq(gidx.levels[0], pidx.levels[0]) + assert_eq(gidx.levels[1], pidx.levels[1])