Skip to content

BUG: Allow plain bools in ExtensionArray.equals #34661

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 3 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,12 @@ def equals(self, other: "ExtensionArray") -> bool:
# boolean array with NA -> fill with False
equal_values = equal_values.fillna(False)
equal_na = self.isna() & other.isna()
return (equal_values | equal_na).all().item()
result = (equal_values | equal_na).all()

if isinstance(result, np.bool_):
Copy link
Contributor

Choose a reason for hiding this comment

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

bool(result) should work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed and CI passed.

return result.item()
else:
return result

def _values_for_factorize(self) -> Tuple[np.ndarray, Any]:
"""
Expand Down
28 changes: 26 additions & 2 deletions pandas/tests/extension/arrow/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import copy
import itertools
import operator
from typing import Type

import numpy as np
Expand Down Expand Up @@ -106,6 +107,27 @@ def astype(self, dtype, copy=True):
def dtype(self):
return self._dtype

def _boolean_op(self, other, op):
if not isinstance(other, type(self)):
raise NotImplementedError()

result = op(np.array(self._data), np.array(other._data))
return ArrowBoolArray(
pa.chunked_array([pa.array(result, mask=pd.isna(self._data.to_pandas()))])
)

def __eq__(self, other):
if not isinstance(other, type(self)):
return False

return self._boolean_op(other, operator.eq)

def __and__(self, other):
return self._boolean_op(other, operator.and_)

def __or__(self, other):
return self._boolean_op(other, operator.or_)

@property
def nbytes(self):
return sum(
Expand Down Expand Up @@ -153,10 +175,12 @@ def _reduce(self, method, skipna=True, **kwargs):
return op(**kwargs)

def any(self, axis=0, out=None):
return self._data.to_pandas().any()
# Explicitly return a plain bool to reproduce GH-34660
return bool(self._data.to_pandas().any())

def all(self, axis=0, out=None):
return self._data.to_pandas().all()
# Explicitly return a plain bool to reproduce GH-34660
return bool(self._data.to_pandas().all())


class ArrowBoolArray(ArrowExtensionArray):
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/extension/arrow/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def data_missing():
return ArrowBoolArray.from_scalars([None, True])


def test_basic_equals(data):
# https://github.com/pandas-dev/pandas/issues/34660
assert pd.Series(data).equals(pd.Series(data))
Copy link
Member

Choose a reason for hiding this comment

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

There is a BaseMethodsTests test_equals that could in theory be reused here. But since none of the other methods are implemented, we would need to skip all other tests, which would be a bit onerous. So fine with a custom test.



class BaseArrowTests:
pass

Expand Down