-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: SparseArray is not implemented as index first #45126
Comments
Please post actual expected outputs, they are just the same as above |
Not the same, pls see indices. |
Ah got you, could you highlight them somehow? |
Yeah, looks like not so very demonstrative example. I will change to some with well seen that contains just one-two values . |
What about SparseDType and fill_value. I dup my comment from mentioned issue Now we have situation when SparseDType keeps both the type of non zero elements and the VALUE of fill_value. The crucial thing of sparse array (as I understand, but I'm not a specialist in this topic) is only non zero values and indices. In Julia, sparse vectors have the type SparseVector{Tv,Ti} where Tv is the type of the stored values and Ti the integer type for the indices. The internal representation is as follows:
Looks like more rational to go on this way. Sure this means dramatically changes in API, but anyway now SparseArray in pandas doesn't looks like working mechanism. @jreback I suggest to change BUG label to API for this issue. Also as I see, one of the main idea of series and this is the crucial part in comparison with just an array is indices. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the master branch of pandas.
Reproducible Example
Issue Description
In current implementation of SparseArray class usually doesn't work with own indices.
When I has starting to work with SparseArray in pandas, I've noticed, that many operations (unary, binary and some methods) work with SparseArray the same as basic ndarray. In some case it uses explicit (to_dense) cast, in the other case it apply operations only to internal sp_values array and doesn't check the indices.
As a result we have wrong SparseArray instances (see examples above). Pay attention that printed arrays are correct, but indices are not.
Anyway in both case the logic doesn't fit the nature of Sparse array.
I suggest to discuss new private api that will be based on sparse logic, because only this way will lead us to effective structure that Sparse Array represents.
There are few open questions about the API:
Deprecation of setting fill_value
I'm not sure that changing fill_value is good point and what is crucial that is the real life case. In the one of examples that I wrote above, e.g. SA that contains both nan and some fill_value (e.g. -1) is looks no natural. I think in such case user should cast own data to using only one fill_value before loading it to SA. See BUG: Sparse structures don't fully support value assignment #21818.
Fill_value dtype should match with SparseDType
See Require the dtype of SparseArray.fill_value and sp_values.dtype to match #23124. Now we can create boolean sparsearray and set fill_value as any value of other type.
Implement index first logic
The idea is that all operation with SparseArray should be based on indices, because as I understand it this is the meaning of sparse array. Here I don't have concrete suggestion. I will try to add something later. Perhaps we should use as example scipy and other frameworks that have such types.
Should operations touch the whole array?
Perhaps any operation should work only with non zero elements. In this case we just to check if some of values became fill_value and in case of true remove it from sp_values and sp_indices.
Links for study:
The text was updated successfully, but these errors were encountered: