You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Roadmap: pandas SparseDataFrame may be deprecated #12800
I noticed in the new roadmap that the second item on the list relates to "Pandas DataFrames and SparseDataFrames". I wanted to mention that according to an August 2018 talk by @datapythonista (who is a pandas core contributor), SparseDataFrames will be deprecated. Here are the slides from the talk (in notebook form).
I mentioned this to @amuelleron Twitter, and he suggested opening an issue to document this.
This PR seems to be the most up-to-date discussion among the pandas team about deprecation.
Just to note: the memory usage of the old SparseDataFrame and the "new" (it's been possible for a while) DataFrame where some of the columns are sparse is identical.
And, I don't think either are that good for very wide matrices. Each column is stored independently as two arrays: One array for the "sparse index" (the positions where the value is explicitly) and the "sparse values" (the value at each sparse index position).
That's essentially equivalent to csc which is still much better than dense.
Yes, update the wording to say that we need to directly support DataFrame
sparsity
So if one would pass into scikit-learn method which accepts "sparse array" as input a DataFrame with SparseSeries, would that work or explode at the moment?
A check_array on a (sparse) DataFrame will convert it to a numpy array (__array__), which for a sparse DataFrame means it gets densified, and memory usage will increase.
You can convert your sparse DataFrame to a scipy sparse matrix, which is probably the best approach for now if you want to keep it sparse.
So currently sparse Pandas DataFrames are supported only if all columns are sparse. What about supporting mixed case where dense columns could be converted to sparse ones?
I don't think trying to guess here is a good idea. I would direct the user to process the dense and sparse subsets independently using a ColumnTrasformer. Or if they really want them processed together then the user should convert the dense to sparse ahead of time.
Activity
justmarkham commentedon Dec 17, 2018
Additional information, that I just noticed on Twitter from @jorisvandenbossche (another pandas core contributor):
amueller commentedon Dec 17, 2018
Thanks for the input.
So it looks like they might
So maybe we should change it to "DataFrames for dense and sparse data" to accommodate what ever solution they are going for
TomAugspurger commentedon Dec 17, 2018
Just to note: the memory usage of the old SparseDataFrame and the "new" (it's been possible for a while) DataFrame where some of the columns are sparse is identical.
And, I don't think either are that good for very wide matrices. Each column is stored independently as two arrays: One array for the "sparse index" (the positions where the value is explicitly) and the "sparse values" (the value at each sparse index position).
jnothman commentedon Dec 18, 2018
mitar commentedon May 26, 2019
So if one would pass into scikit-learn method which accepts "sparse array" as input a DataFrame with SparseSeries, would that work or explode at the moment?
jnothman commentedon May 26, 2019
jorisvandenbossche commentedon May 26, 2019
A
check_arrayon a (sparse) DataFrame will convert it to a numpy array (__array__), which for a sparse DataFrame means it gets densified, and memory usage will increase.You can convert your sparse DataFrame to a scipy sparse matrix, which is probably the best approach for now if you want to keep it sparse.
mitar commentedon May 26, 2019
So for me the beauty of using
DataFrameis that I can have both string columns and numerical columns and throw it at random forest and it just works.Now, with some columns sparse, we cannot just convert it to scipy sparse matrix, because the latter does not support object dtype.
What would be the ideal implementation here to support this properly in scikit-learn?
jnothman commentedon May 26, 2019
amueller commentedon Sep 18, 2019
I agree that we should make it a scipy sparse matrix. Should densifying be considered a bug? also cc @thomasjpfan as a potential todo.
mitar commentedon Mar 27, 2020
So currently sparse Pandas DataFrames are supported only if all columns are sparse. What about supporting mixed case where dense columns could be converted to sparse ones?
thomasjpfan commentedon Apr 5, 2020
What kind of behavior would be reasonable for the mixed case?
mitar commentedon Apr 5, 2020
Making dense columns into sparse columns, I would say, and then everything into sparse?
jnothman commentedon Apr 5, 2020
mitar commentedon Apr 5, 2020
That would be ideal, but it might be hard to know in advance?
TomAugspurger commentedon Apr 6, 2020
I don't think trying to guess here is a good idea. I would direct the user to process the dense and sparse subsets independently using a ColumnTrasformer. Or if they really want them processed together then the user should convert the dense to sparse ahead of time.