Skip to content

Commit

Permalink
BUG: Fix inconsistency between the shape properties of SparseSeries a…
Browse files Browse the repository at this point in the history
…nd SparseArray (#21126) (#21198)

(cherry picked from commit 5348e06)
  • Loading branch information
nprad authored and jorisvandenbossche committed Jun 9, 2018
1 parent 5dfa9fd commit ea737c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ Categorical
- Bug in :func:`pandas.util.testing.assert_index_equal` which raised ``AssertionError`` incorrectly, when comparing two :class:`CategoricalIndex` objects with param ``check_categorical=False`` (:issue:`19776`)
- Bug in :meth:`Categorical.fillna` incorrectly raising a ``TypeError`` when `value` the individual categories are iterable and `value` is an iterable (:issue:`21097`, :issue:`19788`)

Sparse
^^^^^^

- Bug in :attr:`SparseArray.shape` which previously only returned the shape :attr:`SparseArray.sp_values` (:issue:`21126`)

Conversion
^^^^^^^^^^

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ 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] = self.sp_values.__reduce__()[2]
object_state[2] = (object_state[2], subclass_state)
return tuple(object_state)

Expand Down Expand Up @@ -339,6 +340,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
11 changes: 11 additions & 0 deletions pandas/tests/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

@pytest.mark.parametrize('data,shape,dtype', [
([0, 0, 0, 0, 0], (5,), None),
([], (0,), None),
([0], (1,), None),
(['A', 'A', np.nan, 'B'], (4,), np.object)
])
def test_shape(self, data, shape, dtype):
# GH 21126
out = SparseArray(data, dtype=dtype)
assert out.shape == shape

def test_to_dense(self):
vals = np.array([1, np.nan, np.nan, 3, np.nan])
res = SparseArray(vals).to_dense()
Expand Down

0 comments on commit ea737c7

Please sign in to comment.