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
import numpy as np
import pandas as pd
df = pd.DataFrame()
df[0] = [2]
df[1] = pd.arrays.SparseArray([2], fill_value=np.nan, dtype=np.float64)
df.count() # FutureWarning: Allowing arbitrary scalar fill_value in SparseDtype is deprecated
df[0].count() # OK
df[1].count() # OK
series = pd.Series(pd.arrays.SparseArray([2], fill_value=2, dtype=np.float64))
series.count() # OK
Issue Description
FutureWarning
is raised when any object of type pd.Arrays.SparseArray
is contained in a pd.DataFrame
type object. I seem to be explicit about the input parameters of the sparse array and yet I am getting the warning. Something interesting is that this warning is not being displayed when the object itself is not contained within the dataframe, which makes me wonder if this warning is "real" warning or just a false positive. If my SparseArray
is not well defined, then I would expectd that invoking count
on the pd.Series
object that contains the pd.SparseArray
would also trigger a warning.
This warning is being displayed by other methods that involve count
(i,e info()
)
EDIT: It seems this warning is being triggered because when calling count
on the whole Dataframe and this one contains at least one SparseArray
and one "normal" columns, all the types are coherced into a pd.Series[int64]
, which ends up triggering the warning. Therefore, how this can be fixed?
Expected Behavior
No warning is raised
Installed Versions
Comment From: yuanx749
It now appears on main a ValueError is raised.
ValueError: fill_value must be a valid value for the SparseDtype.subtype