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

Avoid DataFrame conversion in MultiIndex.from_pandas #14470

Merged
merged 12 commits into from
Nov 28, 2023

Conversation

mroeschke
Copy link
Contributor

Description

This uncovered a bug where MultiIndex.levels could be a list of Series instead of a list of Index.

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 bug Something isn't working Python Affects Python cuDF API. non-breaking Non-breaking change labels Nov 22, 2023
@mroeschke mroeschke requested a review from a team as a code owner November 22, 2023 00:29
[level._data[column_name]], col
)[0]
for (column_name, col), level in zip(codes._data.items(), levels):
result_col = level._column.take(col)
Copy link
Contributor

Choose a reason for hiding this comment

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

On line 175 we already do partial bounds-checking. The treatment of -1 is AFAICT undocumented pandas behaviour. That is codes do not do normal indexing but instead -1 is for "missing value".

But let us move all of the validation into one place:

import cudf._lib as libcudf
from cudf._lib.types import size_type_dtype
import numpy as np

lo, hi = libcudf.reduce.minmax(code)
if lo.value < -1 or hi.value > len(level) - 1:
    raise ValueError(...)
if lo.value == -1:
    # Now we can gather and insert null automatically
    code[code == -1] = np.iinfo(size_type_dtype).min
result_col, = libcudf.copying.gather([level], code, nullify=True)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice find. Was able to consolidate the codes validate in this loop

@@ -158,7 +159,7 @@ def __init__(
"codes and is inconsistent!"
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, I am not sure that codes can be np.int64 it should probably be size_type_dtype. But let us not fix that here.

Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

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

Minor suggestion to retain the pandas issue reference in the xfail, otherwise looks great, thanks!

python/cudf/cudf/tests/test_multiindex.py Show resolved Hide resolved
python/cudf/cudf/core/multiindex.py Show resolved Hide resolved
python/cudf/cudf/tests/test_dropna.py Outdated Show resolved Hide resolved
@galipremsagar
Copy link
Contributor

@mroeschke can we drop the REF: prefix from the PR title? A while ago we decided not to have prefixes in the PR titles as they will be added to the commit history.

@mroeschke mroeschke changed the title REF: Avoid DataFrame conversion in MultiIndex.from_pandas Avoid DataFrame conversion in MultiIndex.from_pandas Nov 28, 2023
Co-authored-by: Lawrence Mitchell <wence@gmx.li>
@mroeschke
Copy link
Contributor Author

/merge

@rapids-bot rapids-bot bot merged commit d65a2af into rapidsai:branch-24.02 Nov 28, 2023
67 checks passed
@mroeschke mroeschke deleted the ref/mi_init branch November 28, 2023 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working non-breaking Non-breaking change Python Affects Python cuDF API.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants