-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
ENH/TST: Add isin, _hasna for ArrowExtensionArray #47805
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 hidden or 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from pandas.compat import ( | ||
pa_version_under1p01, | ||
pa_version_under2p0, | ||
pa_version_under3p0, | ||
pa_version_under4p0, | ||
pa_version_under5p0, | ||
pa_version_under6p0, | ||
|
@@ -388,6 +389,10 @@ def __len__(self) -> int: | |
""" | ||
return len(self._data) | ||
|
||
@property | ||
def _hasna(self) -> bool: | ||
return self._data.null_count > 0 | ||
|
||
def isna(self) -> npt.NDArray[np.bool_]: | ||
""" | ||
Boolean NumPy array indicating if each value is missing. | ||
|
@@ -425,6 +430,49 @@ def dropna(self: ArrowExtensionArrayT) -> ArrowExtensionArrayT: | |
else: | ||
return type(self)(pc.drop_null(self._data)) | ||
|
||
def isin(self, values): | ||
if pa_version_under2p0: | ||
fallback_performancewarning(version="2") | ||
return super().isin(values) | ||
|
||
# for an empty value_set pyarrow 3.0.0 segfaults and pyarrow 2.0.0 returns True | ||
# for null values, so we short-circuit to return all False array. | ||
if not len(values): | ||
return np.zeros(len(self), dtype=bool) | ||
|
||
kwargs = {} | ||
if pa_version_under3p0: | ||
# in pyarrow 2.0.0 skip_null is ignored but is a required keyword and raises | ||
# with unexpected keyword argument in pyarrow 3.0.0+ | ||
kwargs["skip_null"] = True | ||
|
||
result = pc.is_in( | ||
self._data, value_set=pa.array(values, from_pandas=True), **kwargs | ||
) | ||
# pyarrow 2.0.0 returned nulls, so we explicitly specify dtype to convert nulls | ||
# to False | ||
return np.array(result, dtype=np.bool_) | ||
|
||
def _values_for_factorize(self) -> tuple[np.ndarray, Any]: | ||
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. any test for this? 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. Yup, |
||
""" | ||
Return an array and missing value suitable for factorization. | ||
|
||
Returns | ||
------- | ||
values : ndarray | ||
na_value : pd.NA | ||
|
||
Notes | ||
----- | ||
The values returned by this method are also used in | ||
:func:`pandas.util.hash_pandas_object`. | ||
""" | ||
if pa_version_under2p0: | ||
values = self._data.to_pandas().values | ||
else: | ||
values = self._data.to_numpy() | ||
return values, self.dtype.na_value | ||
|
||
@doc(ExtensionArray.factorize) | ||
def factorize( | ||
self, | ||
|
@@ -622,8 +670,6 @@ def _concat_same_type( | |
------- | ||
ArrowExtensionArray | ||
""" | ||
import pyarrow as pa | ||
|
||
chunks = [array for ea in to_concat for array in ea._data.iterchunks()] | ||
arr = pa.chunked_array(chunks) | ||
return cls(arr) | ||
|
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.
can you type this?