Skip to content

Commit

Permalink
Fix return type of MultiIndex.levels (#13870)
Browse files Browse the repository at this point in the history
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: #13870
  • Loading branch information
galipremsagar authored Aug 14, 2023
1 parent 989c411 commit 65e572d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
13 changes: 4 additions & 9 deletions python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/tests/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

0 comments on commit 65e572d

Please sign in to comment.