Skip to content

Implement cudf.MultiIndex.from_arrays - #14740

Merged
rapids-bot[bot] merged 5 commits into
NVIDIA:branch-24.02from
mroeschke:enh/mi_from_arrays
Jan 18, 2024
Merged

Implement cudf.MultiIndex.from_arrays#14740
rapids-bot[bot] merged 5 commits into
NVIDIA:branch-24.02from
mroeschke:enh/mi_from_arrays

Conversation

@mroeschke

Copy link
Copy Markdown
Contributor

Description

Implements cudf.MultiIndex.from_arrays

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@mroeschke mroeschke added Python Affects Python cuDF API. improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels Jan 11, 2024
@mroeschke
mroeschke requested a review from a team as a code owner January 11, 2024 01:19
@mroeschke
mroeschke requested review from isVoid and vyasr January 11, 2024 01:19
"""
Convert arrays to MultiIndex.

Parameters

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing sortorder docstring in parameters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added


@pytest.mark.parametrize(
"array",
[list, tuple, np.array, pd.Index, cudf.Index, pd.Series, cudf.Series],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test cupy arrays?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Added

Comment on lines +1481 to +1486
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a functional exercise, this can rewrite as a map statement:

code_levels = map(functools.partial(factorize, sort=True), array)
codes, levels = [x[0] for x in code_levels], [x[1] for x in code_levels]

Error checking is also functional:

if not all (is_list_like(arr) or is_column_like(arr) for arr in arrays):
    raise TypeError(error_msg)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya my idea was just to do the validation + factorization in the same loop

@mroeschke

Copy link
Copy Markdown
Contributor Author

/merge

@rapids-bot
rapids-bot Bot merged commit 734ca75 into NVIDIA:branch-24.02 Jan 18, 2024
@mroeschke
mroeschke deleted the enh/mi_from_arrays branch January 18, 2024 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improvement / enhancement to an existing function non-breaking Non-breaking change Python Affects Python cuDF API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants