-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
ENH: Implement more efficient merge for arrow strings #54443
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ | |
is_number, | ||
is_numeric_dtype, | ||
is_object_dtype, | ||
is_string_dtype, | ||
needs_i8_conversion, | ||
) | ||
from pandas.core.dtypes.dtypes import ( | ||
|
@@ -2401,13 +2402,37 @@ def _factorize_keys( | |
if not isinstance(lk, BaseMaskedArray) and not ( | ||
# exclude arrow dtypes that would get cast to object | ||
isinstance(lk.dtype, ArrowDtype) | ||
and is_numeric_dtype(lk.dtype.numpy_dtype) | ||
and ( | ||
is_numeric_dtype(lk.dtype.numpy_dtype) | ||
or is_string_dtype(lk.dtype) | ||
and not sort | ||
) | ||
): | ||
lk, _ = lk._values_for_factorize() | ||
|
||
# error: Item "ndarray" of "Union[Any, ndarray]" has no attribute | ||
# "_values_for_factorize" | ||
rk, _ = rk._values_for_factorize() # type: ignore[union-attr] | ||
elif isinstance(lk.dtype, ArrowDtype) and is_string_dtype(lk.dtype): | ||
import pyarrow as pa | ||
import pyarrow.compute as pc | ||
|
||
len_lk = len(lk) | ||
lk = lk._pa_array # type: ignore[union-attr, attr-defined] | ||
rk = rk._pa_array # type: ignore[union-attr, attr-defined] | ||
dc = pa.concat_arrays( | ||
[lk.combine_chunks(), rk.combine_chunks()] | ||
).dictionary_encode() | ||
lukemanley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
length = len(dc.dictionary) | ||
|
||
llab, rlab, count = ( | ||
pc.fill_null(dc.indices[slice(len_lk)], length).to_numpy(), | ||
pc.fill_null(dc.indices[slice(len_lk, None)], length).to_numpy(), | ||
len(dc.dictionary), | ||
) | ||
if how == "right": | ||
return rlab, llab, count | ||
return llab, rlab, count | ||
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. should the 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. Follow-up. This didn't cover sort. Wanted to get the other PR in first |
||
|
||
if needs_i8_conversion(lk.dtype) and lk.dtype == rk.dtype: | ||
# GH#23917 TODO: Needs tests for non-matching dtypes | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
no kidding my reaction on reading this was to sigh and then say out loud "gross". let's take this opportunity to implement the appropriate EA method
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.
:) As discussed, let's do as a follow up. But I agree that this is needed