-
-
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
Changes from 4 commits
54d5bb8
0538861
089121a
c4bbef4
7cc18c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]) | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -454,6 +454,17 @@ 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): | ||
# GH 21126 | ||
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. can you add cases that have a fill_value, can you parameterize the tests. also add a test with an object array as well (e.g. steal cases from the constructor tests) |
||
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() | ||
|
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 expand on this slightly. maybe
Bzug in SparseArray.shape which previouslvy return a shape that only included the dense values.