Skip to content

BUG: regression in master for DataFrame.sparse.from_spmatrix #59212 #59317

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

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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()
Expand Down
Loading