Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return type of MultiIndex.levels #13870

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])