Skip to content

Commit

Permalink
Preserve array name in MultiIndex.from_arrays (#16515)
Browse files Browse the repository at this point in the history
xref #16507

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - Matthew Murray (https://github.com/Matt711)

URL: #16515
  • Loading branch information
mroeschke authored Aug 9, 2024
1 parent 4cd87d3 commit 45b20d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,12 +1394,16 @@ def from_arrays(
raise TypeError(error_msg)
codes = []
levels = []
names_from_arrays = []
for array in arrays:
if not (is_list_like(array) or is_column_like(array)):
raise TypeError(error_msg)
code, level = factorize(array, sort=True)
codes.append(code)
levels.append(level)
names_from_arrays.append(getattr(array, "name", None))
if names is None:
names = names_from_arrays
return cls(
codes=codes, levels=levels, sortorder=sortorder, names=names
)
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 @@ -2179,3 +2179,13 @@ def test_unique_level():
result = pd_mi.unique(level=1)
expected = cudf_mi.unique(level=1)
assert_eq(result, expected)


@pytest.mark.parametrize(
"idx", [pd.Index, pd.CategoricalIndex, pd.DatetimeIndex, pd.TimedeltaIndex]
)
def test_from_arrays_infer_names(idx):
arrays = [idx([1], name="foo"), idx([2], name="bar")]
expected = pd.MultiIndex.from_arrays(arrays)
result = cudf.MultiIndex.from_arrays(arrays)
assert_eq(result, expected)

0 comments on commit 45b20d1

Please sign in to comment.