-
-
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 6 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 |
---|---|---|
|
@@ -1970,7 +1970,9 @@ def _sort_tuples( | |
return original_values[indexer] | ||
|
||
|
||
def union_with_duplicates(lvals: ArrayLike, rvals: ArrayLike) -> ArrayLike: | ||
def union_with_duplicates( | ||
lvals: ArrayLike | Index, rvals: ArrayLike | Index | ||
) -> ArrayLike: | ||
""" | ||
Extracts the union from lvals and rvals with respect to duplicates and nans in | ||
both arrays. | ||
|
@@ -1991,13 +1993,14 @@ 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) | ||
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) | ||
final_count = np.maximum(l_count, r_count).astype("int", copy=False) | ||
if isinstance(lvals, ABCMultiIndex) and isinstance(rvals, ABCMultiIndex): | ||
unique_vals = lvals.append(rvals).unique() | ||
else: | ||
unique_vals = unique(concat_compat([lvals, rvals])) | ||
unique_vals = ensure_wrapped_if_datetimelike(unique_vals) | ||
repeats = final_count.reindex(unique_vals).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. Could you add the mypy error as a comment? 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. I fixed/removed the mypy ignore |
||
return np.repeat(unique_vals, repeats) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -578,6 +578,30 @@ def test_union_keep_ea_dtype(any_numeric_ea_dtype, val): | |
tm.assert_index_equal(result, expected) | ||
|
||
|
||
def test_union_with_duplicates_keep_ea_dtype(any_numeric_ea_dtype): | ||
# GH48900 | ||
mi1 = MultiIndex.from_arrays( | ||
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. What happens if theres duplicate 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. Works the same as non-NA. I added the dupe NA case to the test. |
||
[ | ||
Series([1, 3, 2], dtype=any_numeric_ea_dtype), | ||
Series([1, 3, 2], dtype=any_numeric_ea_dtype), | ||
] | ||
) | ||
mi2 = MultiIndex.from_arrays( | ||
[ | ||
Series([2, 3, 3], dtype=any_numeric_ea_dtype), | ||
Series([2, 3, 3], dtype=any_numeric_ea_dtype), | ||
] | ||
) | ||
result = mi1.union(mi2) | ||
expected = MultiIndex.from_arrays( | ||
[ | ||
Series([1, 2, 3, 3], dtype=any_numeric_ea_dtype), | ||
Series([1, 2, 3, 3], dtype=any_numeric_ea_dtype), | ||
] | ||
) | ||
tm.assert_index_equal(result, expected) | ||
|
||
|
||
def test_union_duplicates(index, request): | ||
# GH#38977 | ||
if index.empty or isinstance(index, (IntervalIndex, CategoricalIndex)): | ||
|
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.