-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: ufunc is not applied to sparse.fill_value #13853
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
Conversation
Current coverage is 85.27% (diff: 93.33%)@@ master #13853 diff @@
==========================================
Files 139 139
Lines 50020 50031 +11
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 42657 42666 +9
- Misses 7363 7365 +2
Partials 0 0
|
tm.assert_sp_array_equal(np.abs(sparse), result) | ||
|
||
sparse = SparseArray([1, -1, 2, -2], fill_value=1) | ||
result = SparseArray([1, 2, 2], sparse_index=sparse.sp_index, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why this result is correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because assert_sp_array_equal
compares sparse internal representation, it is for prepare correct internal repr. You can see the result is correct from its dense repr.
# test case
sparse = pd.SparseArray([1, -1, 2, -2], fill_value=1)
abs(sparse).to_dense()
# array([ 1., 1., 2., 2.])
# result
pd.SparseArray([1, 2, 2], sparse_index=sparse.sp_index, fill_value=1).to_dense()
# array([ 1., 1., 2., 2.])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, good to know. I wasn't 100% clear on how the sparse comparison worked. Thanks!
LGTM cc @jreback |
@@ -213,6 +213,17 @@ def kind(self): | |||
elif isinstance(self.sp_index, IntIndex): | |||
return 'integer' | |||
|
|||
def __array_wrap__(self, out_arr, context=None): | |||
if isinstance(context, tuple) and len(context) == 3: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put a comment here of what this is doing
I understand why this is needed, but it feels a tad unnatural. I am not sure a user will be expecting that the fill value will have the ufunc be applied here. Can we add a section to the docs showing this? |
Sure, added small section. |
git diff upstream/master | flake8 --diff
When ufunc is applied to sparse, it is not applied to
fill_value
. Thus results are incorrect.on current master:
cc @gfyoung