diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 1627a90fc6ac0..b7508a434828a 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -622,7 +622,7 @@ Reshaping Sparse ^^^^^^ - Bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`) -- Bug in :meth:`DataFrame.sparse.from_spmatrix` which hard coded an invalid ``fill_value`` for certain subtypes. (:issue:`59063`) +- Bug in :meth:`DataFrame.sparse.from_spmatrix` which hard coded an invalid ``fill_value`` for certain subtypes. (:issue:`59063`, :issue:`59212`) ExtensionArray ^^^^^^^^^^^^^^ diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 3aeab96e03163..a851e6afef50f 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1682,8 +1682,8 @@ class SparseDtype(ExtensionDtype): =========== ========== dtype na_value =========== ========== - float ``np.nan`` - complex ``np.nan`` + float ``0.0`` + complex ``0.0 + 0.0j`` int ``0`` bool ``False`` datetime64 ``pd.NaT`` @@ -1747,6 +1747,14 @@ def __init__(self, dtype: Dtype = np.float64, fill_value: Any = None) -> None: if fill_value is None: fill_value = na_value_for_dtype(dtype) + # Default values for float type is NaN. Hence, in order to create a Sparse + # matrix of type float, we need to override this default value as 0.0. + if dtype.kind in "f": + fill_value = 0.0 + # Similarly, default value needs to be overridden for complex type arrays. + elif dtype.kind in "c": + fill_value = 0.0 + 0.0j + self._dtype = dtype self._fill_value = fill_value self._check_fill_value()