Skip to content

SparseArray compatibility issues in e.g. sklearn. Series.shape and SparseArray.shape output not consistent. #21126

Closed
@Xparx

Description

@Xparx

Code Sample

import pandas as pd
import numpy as np
spdf = pd.DataFrame(np.random.rand(5,5)>0.7).astype(float).to_sparse(fill_value=0)
spdf[1].shape[0] == spdf[1].values.shape[0]

Problem description

Using packages that internally translate pandas dataframes to arrays (like sklearn) requires vectors and matrices to be specific sizes. Using sparse data matrices in pandas produces unpredictable results as internals of sklearn will translate pandas to SparseArray arrays and check sizes. The size of spdf[1].values.shape is not guaranteed to have the expected shape output.

Also depending on the order of columns selection (after or before applying values) gives different results as the output of spdf.values is a numpy.ndarray.

Expected Output

I would expect the last statement in the code above to return True for all vectors in spdf.
I'm not sure this is a bug or intended feature but it struck me as weird as well as inconvenient when working with different libraries.
I encountered this running sklearn.linear_model.Lasso with a sparse target vector.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.6.0-040600-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.23.0
pytest: None
pip: 10.0.1
setuptools: 39.1.0
Cython: None
numpy: 1.14.2
scipy: 1.0.1
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.5.0
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: 4.1.1
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    SparseSparse Data Type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions