Skip to content

Fix SparseDtype comparison #57783

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 4 commits into from
Mar 9, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Performance improvements

Bug fixes
~~~~~~~~~
- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,17 +1705,15 @@ def __eq__(self, other: object) -> bool:

if isinstance(other, type(self)):
subtype = self.subtype == other.subtype
if self._is_na_fill_value:
if self._is_na_fill_value or other._is_na_fill_value:
# this case is complicated by two things:
# SparseDtype(float, float(nan)) == SparseDtype(float, np.nan)
# SparseDtype(float, np.nan) != SparseDtype(float, pd.NaT)
# i.e. we want to treat any floating-point NaN as equal, but
# not a floating-point NaN and a datetime NaT.
fill_value = (
other._is_na_fill_value
and isinstance(self.fill_value, type(other.fill_value))
or isinstance(other.fill_value, type(self.fill_value))
)
fill_value = isinstance(
self.fill_value, type(other.fill_value)
) or isinstance(other.fill_value, type(self.fill_value))
else:
with warnings.catch_warnings():
# Ignore spurious numpy warning
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/sparse/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def test_nans_equal():
assert b == a


def test_nans_not_equal():
# GH 54770
a = SparseDtype(float, 0)
b = SparseDtype(float, pd.NA)
assert a != b
assert b != a


with warnings.catch_warnings():
msg = "Allowing arbitrary scalar fill_value in SparseDtype is deprecated"
warnings.filterwarnings("ignore", msg, category=FutureWarning)
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,6 @@ def test_fillna_limit_backfill(self, data_missing):
super().test_fillna_limit_backfill(data_missing)

def test_fillna_no_op_returns_copy(self, data, request):
if np.isnan(data.fill_value):
request.applymarker(
pytest.mark.xfail(reason="returns array with different fill value")
)
super().test_fillna_no_op_returns_copy(data)

@pytest.mark.xfail(reason="Unsupported")
Expand Down Expand Up @@ -400,6 +396,8 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
"rmul",
"floordiv",
"rfloordiv",
"truediv",
"rtruediv",
"pow",
"mod",
"rmod",
Expand Down