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

print("---Both columns are sparse---")
    a = np.random.randn(10)
    a[2:5] = np.nan
    a[7:8] = np.nan
    sp_a = pd.arrays.SparseArray(a)
    b = np.random.randn(10)
    b[2:5] = np.nan
    b[7:8] = np.nan
    sp_b = pd.arrays.SparseArray(b)
    df1 = pd.DataFrame({"a": sp_a, "b": sp_b})
    print(df1.dtypes)
    print("df1.sparse.to_dense() will succeed")
    print(df1.sparse.to_dense())

    print("---One column is sparse---")
    df2 = pd.DataFrame({"a": sp_a, "b": b})
    print(df2.dtypes)
    print("df2.sparse.to_dense() will fail")
    print(df2.sparse.to_dense())

Issue Description

I have a pandas DataFrame where some columns are dense (float, integer, string, etc.) and some columns are sparse floats or sparse ints. I want to convert the entire DataFrame to dense. However, DataFrame.sparse.to_dense() fails with the error AttributeError: Can only use the '.sparse' accessor with Sparse data.

I'm using pandas 1.5.0.

Expected Behavior

The expected behavior is for pandas to return a dense DataFrame. If no columns are sparse, then the DataFrame is unchanged. If one or more columns are sparse, then the sparse columns are converted to dense and returned (and any non-sparse columns are unchanged).

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e38bafe7300a6003a1d18bd80f3f77c763 python : 3.10.6.final.0 python-bits : 64 OS : Darwin OS-release : 21.6.0 Version : Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : None LOCALE : en_US.UTF-8

pandas : 1.5.0 numpy : 1.22.3 pytz : 2022.2.1 dateutil : 2.8.2 setuptools : 63.4.3 pip : 22.2.2 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 4.9.1 html5lib : None pymysql : 1.0.2 psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : 4.11.1 bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : 3.0.10 pandas_gbq : None pyarrow : 8.0.0 pyreadstat : None pyxlsb : None s3fs : None scipy : 1.8.0 snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : 2022.4 None

Comment From: phofl

Hi, thanks for your report.

This behaves as intended. We would call to_dense on all columns, which would fail later on. You have to do the conversion yourself