Skip to content

Commit d7e1458

Browse files
committed
Fix SparseDtype comparison
1 parent 7287487 commit d7e1458

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

pandas/core/dtypes/dtypes.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1705,17 +1705,15 @@ def __eq__(self, other: object) -> bool:
17051705

17061706
if isinstance(other, type(self)):
17071707
subtype = self.subtype == other.subtype
1708-
if self._is_na_fill_value:
1708+
if self._is_na_fill_value or other._is_na_fill_value:
17091709
# this case is complicated by two things:
17101710
# SparseDtype(float, float(nan)) == SparseDtype(float, np.nan)
17111711
# SparseDtype(float, np.nan) != SparseDtype(float, pd.NaT)
17121712
# i.e. we want to treat any floating-point NaN as equal, but
17131713
# not a floating-point NaN and a datetime NaT.
1714-
fill_value = (
1715-
other._is_na_fill_value
1716-
and isinstance(self.fill_value, type(other.fill_value))
1717-
or isinstance(other.fill_value, type(self.fill_value))
1718-
)
1714+
fill_value = isinstance(
1715+
self.fill_value, type(other.fill_value)
1716+
) or isinstance(other.fill_value, type(self.fill_value))
17191717
else:
17201718
with warnings.catch_warnings():
17211719
# Ignore spurious numpy warning

pandas/tests/arrays/sparse/test_dtype.py

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def test_nans_equal():
6868
assert b == a
6969

7070

71+
def test_nans_not_equal():
72+
# GH 54770
73+
a = SparseDtype(float, 0)
74+
b = SparseDtype(float, pd.NA)
75+
assert a != b
76+
assert b != a
77+
78+
7179
with warnings.catch_warnings():
7280
msg = "Allowing arbitrary scalar fill_value in SparseDtype is deprecated"
7381
warnings.filterwarnings("ignore", msg, category=FutureWarning)

0 commit comments

Comments
 (0)