-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH/TST: Add BaseInterfaceTests tests for ArrowExtensionArray #47377
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2c984c2
ENH/TST: Add BaseInterfaceTests tests for ArrowExtensionArray
mroeschke a766ed1
Fix mock ArrowExtensionArray
mroeschke 3b01b77
Merge remote-tracking branch 'upstream/main' into enh/arrow_interface…
mroeschke e09ac0a
Pyarrow compat
mroeschke 7ca44ae
Merge remote-tracking branch 'upstream/main' into enh/arrow_interface…
mroeschke 2c06f26
Add BaseMissingTests too
mroeschke 607baed
Add setitem tests too
mroeschke 5e7af38
Merge remote-tracking branch 'upstream/main' into enh/arrow_interface…
mroeschke 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 |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
) | ||
from pandas.core.dtypes.missing import isna | ||
|
||
from pandas.core.arraylike import OpsMixin | ||
from pandas.core.arrays.base import ExtensionArray | ||
from pandas.core.indexers import ( | ||
check_array_indexer, | ||
|
@@ -45,13 +46,22 @@ | |
from pandas.core.arrays.arrow._arrow_utils import fallback_performancewarning | ||
from pandas.core.arrays.arrow.dtype import ArrowDtype | ||
|
||
ARROW_CMP_FUNCS = { | ||
"eq": pc.equal, | ||
"ne": pc.not_equal, | ||
"lt": pc.less, | ||
"gt": pc.greater, | ||
"le": pc.less_equal, | ||
"ge": pc.greater_equal, | ||
} | ||
|
||
if TYPE_CHECKING: | ||
from pandas import Series | ||
|
||
ArrowExtensionArrayT = TypeVar("ArrowExtensionArrayT", bound="ArrowExtensionArray") | ||
|
||
|
||
class ArrowExtensionArray(ExtensionArray): | ||
class ArrowExtensionArray(OpsMixin, ExtensionArray): | ||
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 I be using 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 recommend OpsMixin |
||
""" | ||
Base class for ExtensionArray backed by Arrow ChunkedArray. | ||
""" | ||
|
@@ -179,6 +189,34 @@ def __arrow_array__(self, type=None): | |
"""Convert myself to a pyarrow ChunkedArray.""" | ||
return self._data | ||
|
||
def _cmp_method(self, other, op): | ||
from pandas.arrays import BooleanArray | ||
|
||
pc_func = ARROW_CMP_FUNCS[op.__name__] | ||
if isinstance(other, ArrowExtensionArray): | ||
result = pc_func(self._data, other._data) | ||
elif isinstance(other, (np.ndarray, list)): | ||
result = pc_func(self._data, other) | ||
elif is_scalar(other): | ||
try: | ||
result = pc_func(self._data, pa.scalar(other)) | ||
except (pa.lib.ArrowNotImplementedError, pa.lib.ArrowInvalid): | ||
mask = isna(self) | isna(other) | ||
valid = ~mask | ||
result = np.zeros(len(self), dtype="bool") | ||
result[valid] = op(np.array(self)[valid], other) | ||
return BooleanArray(result, mask) | ||
else: | ||
return NotImplementedError( | ||
f"{op.__name__} not implemented for {type(other)}" | ||
) | ||
|
||
if pa_version_under2p0: | ||
result = result.to_pandas().values | ||
else: | ||
result = result.to_numpy() | ||
return BooleanArray._from_sequence(result) | ||
|
||
def equals(self, other) -> bool: | ||
if not isinstance(other, ArrowExtensionArray): | ||
return False | ||
|
@@ -581,7 +619,7 @@ def _replace_with_indices( | |
# fast path for a contiguous set of indices | ||
arrays = [ | ||
chunk[:start], | ||
pa.array(value, type=chunk.type), | ||
pa.array(value, type=chunk.type, from_pandas=True), | ||
chunk[stop + 1 :], | ||
] | ||
arrays = [arr for arr in arrays if len(arr)] | ||
|
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
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
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
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.
isn't there a min version on some of these?
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.
Looks like they all have been around since pyarrow version 2 which I think is the version we require to use these functions
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.
Plus we catch the
except (pa.lib.ArrowNotImplementedError, pa.lib.ArrowInvalid)
when calling these with fallback behavior.