-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG/PERF: algos.union_with_duplicates losing EA dtypes #48900
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
Changes from 3 commits
6760a19
0d8237f
5d32385
eadf5f1
3b0adbe
5180e1a
8df7a38
335614d
51bc249
e466ade
9c2a2a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1991,13 +1991,11 @@ def union_with_duplicates(lvals: ArrayLike, rvals: ArrayLike) -> ArrayLike: | |
----- | ||
Caller is responsible for ensuring lvals.dtype == rvals.dtype. | ||
""" | ||
indexer = [] | ||
l_count = value_counts(lvals, dropna=False) | ||
r_count = value_counts(rvals, dropna=False) | ||
l_count, r_count = l_count.align(r_count, fill_value=0) | ||
final_count = np.maximum(l_count, r_count).astype("int", copy=False) | ||
unique_array = unique(concat_compat([lvals, rvals])) | ||
unique_array = ensure_wrapped_if_datetimelike(unique_array) | ||
|
||
for i, value in enumerate(unique_array): | ||
indexer += [i] * int(max(l_count.at[value], r_count.at[value])) | ||
return unique_array.take(indexer) | ||
repeats = final_count.reindex(unique_array).values # type: ignore[attr-defined] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good, can you refactor this to return the indexer and operate on the MultiIndex in _union()? This avoids losing the dtype. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored to preserve EA dtypes. The indexer applies to the combined unique values not the original MI's so I don't think we need to return it. |
||
return np.repeat(unique_array, repeats) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've got 2 MultiIndex checks now. I'd rather handle this on the _union level. Is there any performance benefit of doing it like this?
Additionally, I think you can use unique_vals.take(repeats)
That's more in line with what we are doing elsewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need the MultiIndex checks within union_with_duplicates, otherwise we lose dtypes in there. I'm not sure how how this could be done only on the _union level. Let me know if you have any suggestions.
The repeat logic is not quite a simple
take
:is equivalent to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could simply return
final_count
and handle the rest In the _union level. Alternatively, you could just pass in self and other and handle everything on the lower level. Or add another argumentunique_vals
to the functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, makes sense. I pushed the type checking down into union_with_duplicates.