Open bmreiniger opened 4 months ago
take
The difference comes from the fact that the default fill value of numpy float64 is 0.0 whereas for pandas it is NaN. I tried changing it for the from_spmatrix
function, but it introduces breaking changes for the rest of the Sparse matrix functions.
It's my impression that scipy does everything assuming that the fill value is zero, so that when creating a dataframe from a scipy sparse array pandas should do the same. If that's the case, should this function just override the usual default pandas fill value for float type scipy sparse?
This is a tough one. We have two different ways of handling sparse values depending on the underlying type:
fill-with-zero-value could work for ints, floats, and complex (and as you noted, this is how NumPy works), but that same logic would not extend to datetime types, since a 0 value there is still a valid value. On the other hand, we don't support missing values for ints and bools well (at least not using our historical NumPy types, and not without PDEP-16, so fill-with-zero-value has to be used for those
In our current state I don't see any way that we can make everyone happy. In the future, using a missing value for the sparse elements is the only way that could work across all types. So I'm inclined to say we should leave the current behavior as is since it more closely represents where I think we need to be long term, but @mroeschke @jorisvandenbossche @christopher-titchen may have thoughts as well
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
(This bug does not exist on the latest version of pandas, but that checkbox was required to submit the issue.)
In main branch, the example produces a frame with entries 1.0 and NaN (sparse, with
fill_value
nan).PR #59064 corrected a bug for other dtypes, but introduced this regression for float dtype.
Expected Behavior
In latest version (2.2.2) and earlier, produces a frame with entries 1.0 and 0.0 (sparse, with
fill_value
0).Installed Versions