Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Xparx opened this issue May 18, 2018 · 3 comments
Labels
Milestone

Comments

@Xparx
Copy link

@Xparx Xparx commented May 18, 2018

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

@TomAugspurger
Copy link
Contributor

@TomAugspurger TomAugspurger commented May 18, 2018

Seems like we inherit shape from ndarray. Should be enough to override it with

@property
def shape(self):
    return (len(self),)

Interested in making a PR?

@TomAugspurger TomAugspurger added this to the Next Major Release milestone May 18, 2018
@nprad
Copy link
Contributor

@nprad nprad commented May 19, 2018

@Xparx. Let me know if you're going to work on this. If not, I would be interested in picking this ticket up.

@Xparx
Copy link
Author

@Xparx Xparx commented May 19, 2018

@nprad. Feel free to pick this up.

nprad added a commit to nprad/pandas that referenced this issue May 25, 2018
nprad added a commit to nprad/pandas that referenced this issue May 25, 2018
nprad added a commit to nprad/pandas that referenced this issue May 25, 2018
nprad added a commit to nprad/pandas that referenced this issue May 25, 2018
@jreback jreback modified the milestones: Next Major Release, 0.23.1 May 29, 2018
jreback added a commit that referenced this issue May 31, 2018
jorisvandenbossche added a commit to jorisvandenbossche/pandas that referenced this issue Jun 8, 2018
jorisvandenbossche added a commit that referenced this issue Jun 9, 2018
david-liu-brattle-1 added a commit to david-liu-brattle-1/pandas that referenced this issue Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants