Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Contributor

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?

Copy link
Contributor Author

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 overridden shape does not return the correct value. Therefore, I had to set the shape manually.

object_state[2][1] = super(SparseArray, self).shape
Copy link
Contributor

Choose a reason for hiding this comment

The 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):
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Expand Down