Skip to content

(fix): equality check against singleton PandasExtensionArray #9032

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
May 22, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions xarray/core/extension_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ def __setitem__(self, key, val):
self.array[key] = val

def __eq__(self, other):
if np.isscalar(other):
other = type(self)(type(self.array)([other]))
Copy link
Contributor

Choose a reason for hiding this comment

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

so we just defer to pandas to handle the scalar case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be the idea.

if isinstance(other, PandasExtensionArray):
return self.array == other.array
return self.array == other
Expand Down
13 changes: 9 additions & 4 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_concatenate_extension_duck_array(self, categorical1, categorical2):
).all()

@requires_pyarrow
def test_duck_extension_array_pyarrow_concatenate(self, arrow1, arrow2):
def test_extension_array_pyarrow_concatenate(self, arrow1, arrow2):
concatenated = concatenate(
(PandasExtensionArray(arrow1), PandasExtensionArray(arrow2))
)
Expand Down Expand Up @@ -1024,19 +1024,24 @@ def test_push_dask():
np.testing.assert_equal(actual, expected)


def test_duck_extension_array_equality(categorical1, int1):
def test_extension_array_equality(categorical1, int1):
int_duck_array = PandasExtensionArray(int1)
categorical_duck_array = PandasExtensionArray(categorical1)
assert (int_duck_array != categorical_duck_array).all()
assert (categorical_duck_array == categorical1).all()
assert (int_duck_array[0:2] == int1[0:2]).all()


def test_duck_extension_array_repr(int1):
def test_extension_array_singleton_equality(categorical1):
categorical_duck_array = PandasExtensionArray(categorical1)
assert (categorical_duck_array != "cat3").all()


def test_extension_array_repr(int1):
int_duck_array = PandasExtensionArray(int1)
assert repr(int1) in repr(int_duck_array)


def test_duck_extension_array_attr(int1):
def test_extension_array_attr(int1):
int_duck_array = PandasExtensionArray(int1)
assert (~int_duck_array.fillna(10)).all()
Loading