From 71da82135b1e4091173b6e3579ff144285177337 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Fri, 8 Mar 2024 19:47:48 +0100 Subject: [PATCH 1/4] Fix SparseDtype comparison --- pandas/core/dtypes/dtypes.py | 10 ++++------ pandas/tests/arrays/sparse/test_dtype.py | 8 ++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 2bb2556c88204..f94d32a3b8547 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -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 diff --git a/pandas/tests/arrays/sparse/test_dtype.py b/pandas/tests/arrays/sparse/test_dtype.py index 6fcbfe96a3df7..6f0d41333f2fd 100644 --- a/pandas/tests/arrays/sparse/test_dtype.py +++ b/pandas/tests/arrays/sparse/test_dtype.py @@ -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) From 92a6a7ec657a4ee96d286a97917cabd548e782fe Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Fri, 8 Mar 2024 20:50:31 +0100 Subject: [PATCH 2/4] Fix tests --- pandas/tests/extension/test_sparse.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index c3a1d584170fb..cbca306ab0041 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -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") @@ -400,6 +396,8 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request): "rmul", "floordiv", "rfloordiv", + "truediv", + "rtruediv", "pow", "mod", "rmod", From 2171c82361d9b4e01309d0655adf1e85776d6bed Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Sat, 9 Mar 2024 07:25:55 +0100 Subject: [PATCH 3/4] Add whatsnew --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index ca2ca07ff2fae..c17e8cb764d29 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -285,6 +285,7 @@ Bug fixes - Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`) - Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) - Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`) +- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`) Categorical ^^^^^^^^^^^ From 35039d2f06b5c3bd8987885cd59aafb1eec00d0f Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Sat, 9 Mar 2024 08:58:03 +0100 Subject: [PATCH 4/4] Fix --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index c17e8cb764d29..16be9e0a4fc34 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -280,12 +280,12 @@ 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`) - Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) - Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`) -- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`) Categorical ^^^^^^^^^^^