Skip to content

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 2 commits into from
Jul 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -425,6 +430,49 @@ def dropna(self: ArrowExtensionArrayT) -> ArrowExtensionArrayT:
else:
return type(self)(pc.drop_null(self._data))

def isin(self: ArrowExtensionArrayT, values) -> npt.NDArray[np.bool_]:
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]:
Copy link
Contributor

Choose a reason for hiding this comment

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

any test for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, test_arrow.py::BaseMethodsTests::test_hash_pandas_object_works hits this line

"""
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,
Expand Down Expand Up @@ -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)
Expand Down