Skip to content

Commit 2ca5b18

Browse files
committed
BUG: Fix inconsistency between the shape properties of SparseSeries and SparseArray (pandas-dev#21126)
1 parent a5c02d5 commit 2ca5b18

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/core/sparse/array.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ def __reduce__(self):
290290
"""Necessary for making this object picklable"""
291291
object_state = list(np.ndarray.__reduce__(self))
292292
subclass_state = self.fill_value, self.sp_index
293-
object_state[2] = (object_state[2], subclass_state)
293+
object_state[2] = list(object_state[2])
294+
object_state[2][1] = super(SparseArray, self).shape
295+
object_state[2] = (tuple(object_state[2]), subclass_state)
294296
return tuple(object_state)
295297

296298
def __setstate__(self, state):
@@ -339,6 +341,10 @@ def values(self):
339341
output.put(int_index.indices, self)
340342
return output
341343

344+
@property
345+
def shape(self):
346+
return (len(self),)
347+
342348
@property
343349
def sp_values(self):
344350
# caching not an option, leaks memory

pandas/tests/sparse/test_array.py

+10
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,16 @@ def test_values_asarray(self):
454454
assert_almost_equal(self.arr.to_dense(), self.arr_data)
455455
assert_almost_equal(self.arr.sp_values, np.asarray(self.arr))
456456

457+
def test_shape(self):
458+
out = SparseArray([0, 0, 0, 0, 0])
459+
assert out.shape == (5,)
460+
461+
out = SparseArray([])
462+
assert out.shape == (0,)
463+
464+
out = SparseArray([0])
465+
assert out.shape == (1,)
466+
457467
def test_to_dense(self):
458468
vals = np.array([1, np.nan, np.nan, 3, np.nan])
459469
res = SparseArray(vals).to_dense()

0 commit comments

Comments
 (0)