-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix inconsistency between the shape properties of SparseSeries and SparseArray (#21126) #21198
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
54d5bb8
BUG: Fix inconsistency between the shape properties of SparseSeries a…
nprad 0538861
Added issue number to test_shape
nprad 089121a
Merge remote-tracking branch 'upstream/master' into fix_shape
nprad c4bbef4
Added what's new entry
nprad 7cc18c5
Refactored __reduce__, parametrized tests, and reworded release doc
nprad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,7 +290,9 @@ def __reduce__(self): | |
"""Necessary for making this object picklable""" | ||
object_state = list(np.ndarray.__reduce__(self)) | ||
subclass_state = self.fill_value, self.sp_index | ||
object_state[2] = (object_state[2], subclass_state) | ||
object_state[2] = list(object_state[2]) | ||
object_state[2][1] = super(SparseArray, self).shape | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is pretty obtuse, can you make this any more clear |
||
object_state[2] = (tuple(object_state[2]), subclass_state) | ||
return tuple(object_state) | ||
|
||
def __setstate__(self, state): | ||
|
@@ -339,6 +341,10 @@ def values(self): | |
output.put(int_index.indices, self) | ||
return output | ||
|
||
@property | ||
def shape(self): | ||
return (len(self),) | ||
|
||
@property | ||
def sp_values(self): | ||
# caching not an option, leaks memory | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -454,6 +454,16 @@ def test_values_asarray(self): | |
assert_almost_equal(self.arr.to_dense(), self.arr_data) | ||
assert_almost_equal(self.arr.sp_values, np.asarray(self.arr)) | ||
|
||
def test_shape(self): | ||
out = SparseArray([0, 0, 0, 0, 0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reference the github issue here as a comment. |
||
assert out.shape == (5,) | ||
|
||
out = SparseArray([]) | ||
assert out.shape == (0,) | ||
|
||
out = SparseArray([0]) | ||
assert out.shape == (1,) | ||
|
||
def test_to_dense(self): | ||
vals = np.array([1, np.nan, np.nan, 3, np.nan]) | ||
res = SparseArray(vals).to_dense() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just curious: why the changes here? Did we have tests that failed without it?
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.
Yes,
test_pickle
was failing.shape
is used to pickle objects and the overriddenshape
does not return the correct value. Therefore, I had to set theshape
manually.