pandas icon indicating copy to clipboard operation
pandas copied to clipboard

BUG: `pd.Arrays.SparseArray` warning inconsistencies when contained in a `pd.DataFrame` (potential false positive)

Open mflova opened this issue 9 months ago • 0 comments

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

INSTALLED VERSIONS

commit : d9cdd2ee5a58015ef6f4d15c7226110c9aab8140 python : 3.10.11.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19045 machine : AMD64 processor : Intel64 Family 6 Model 151 Stepping 2, GenuineIntel byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : English_United Kingdom.1252

pandas : 2.2.2 numpy : 1.26.4 pytz : 2024.1 dateutil : 2.9.0.post0 setuptools : 69.5.1 pip : 23.3.1 Cython : None pytest : 7.4.4 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.8.4 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.13.0 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2024.1 qtpy : None pyqt5 : None

mflova avatar May 05 '24 18:05 mflova