Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,21 @@ def value_counts(self, dropna: bool = True) -> Series:

return Series(counts, index=index)

@doc(ExtensionArray.equals)
def equals(self, other) -> bool:
if type(self) != type(other):
return False
if other.dtype != self.dtype:
return False

# GH#44382 if e.g. self[1] is np.nan and other[1] is pd.NA, we are NOT
# equal.
return np.array_equal(self._mask, other._mask) and np.array_equal(
self._data[~self._mask],
other._data[~other._mask],
equal_nan=True,
)

def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
if name in {"any", "all"}:
return getattr(self, name)(skipna=skipna, **kwargs)
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/arrays/floating/test_comparison.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy as np
import pytest

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import FloatingArray
from pandas.tests.arrays.masked_shared import (
ComparisonOps,
NumericOps,
Expand Down Expand Up @@ -34,3 +36,27 @@ def test_equals():
a1 = pd.array([1, 2, None], dtype="Float64")
a2 = pd.array([1, 2, None], dtype="Float32")
assert a1.equals(a2) is False


def test_equals_nan_vs_na():
# GH#44382

mask = np.zeros(3, dtype=bool)
data = np.array([1.0, np.nan, 3.0], dtype=np.float64)

left = FloatingArray(data, mask)
assert left.equals(left)
Copy link
Member

Choose a reason for hiding this comment

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

Could you do something like left2 = FloatingArray(data.copy(), mask.copy()) to create the other array to test equality with? I would just avoid the special case of "identical" objects (in case we might add a fastpath for that in the future)

Copy link
Member Author

Choose a reason for hiding this comment

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

updated; is this what you had in mind?

Copy link
Member

Choose a reason for hiding this comment

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

That's fine!
Can you add a small whatsnew note?

tm.assert_extension_array_equal(left, left)

mask2 = np.array([False, True, False], dtype=bool)
data2 = np.array([1.0, 2.0, 3.0], dtype=np.float64)
right = FloatingArray(data2, mask2)
assert right.equals(right)
tm.assert_extension_array_equal(right, right)

assert not left.equals(right)

# with mask[1] = True, the only difference is data[1], which should
# not matter for equals
mask[1] = True
assert left.equals(right)