Skip to content

Commit eafc94c

Browse files
committed
API: SparseSeries comparison now returns sparse
1 parent 453bc26 commit eafc94c

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

doc/source/whatsnew/v0.19.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ Note that the limitation is applied to ``fill_value`` which default is ``np.nan`
814814
- Bug in ``SparseSeries.abs`` incorrectly keeps negative ``fill_value`` (:issue:`13853`)
815815
- Bug in single row slicing on multi-type ``SparseDataFrame``s, types were previously forced to float (:issue:`13917`)
816816
- Bug in sparse indexing using ``SparseArray`` with ``bool`` dtype may return incorrect result (:issue:`13985`)
817+
- Bug in ``SparseArray`` created from ``SparseSeries`` may lose ``dtype`` (:issue:`13999`)
818+
- Bug in ``SparseSeries`` comparison with dense returns normal ``Series`` rather than ``SparseSeries`` (:issue:`13999`)
819+
817820

818821
.. _whatsnew_0190.indexer_dtype:
819822

pandas/sparse/array.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,17 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
188188
values.fill(data)
189189
data = values
190190

191+
if isinstance(data, ABCSparseSeries):
192+
data = data.values
193+
is_sparse_array = isinstance(data, SparseArray)
194+
191195
if dtype is not None:
192196
dtype = np.dtype(dtype)
193-
is_sparse_array = isinstance(data, SparseArray)
197+
if is_sparse_array:
198+
# temp, always inherit passed SparseArray dtype
199+
# can be removed after GH 13849
200+
dtype = data.dtype
201+
194202
if fill_value is None:
195203
if is_sparse_array:
196204
fill_value = data.fill_value
@@ -211,7 +219,6 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
211219
raise AssertionError("Non array-like type {0} must have"
212220
" the same length as the"
213221
" index".format(type(values)))
214-
215222
# Create array, do *not* copy data by default
216223
if copy:
217224
try:

pandas/sparse/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ def from_coo(cls, A, dense_index=False):
806806
# overwrite basic arithmetic to use SparseSeries version
807807
# force methods to overwrite previous definitions.
808808
ops.add_special_arithmetic_methods(SparseSeries, _arith_method,
809-
comp_method=None,
809+
comp_method=_arith_method,
810810
bool_method=None, use_numexpr=False,
811811
force=True)
812812

pandas/sparse/tests/test_arithmetics.py

-5
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,6 @@ class TestSparseSeriesArithmetic(TestSparseArrayArithmetics):
358358
def _assert(self, a, b):
359359
tm.assert_series_equal(a, b)
360360

361-
def _check_bool_result(self, res):
362-
# ToDo: Must return SparseSeries after GH 667
363-
tm.assertIsInstance(res, self._base)
364-
self.assertEqual(res.dtype, np.bool)
365-
366361
def test_alignment(self):
367362
da = pd.Series(np.arange(4))
368363
db = pd.Series(np.arange(4), index=[1, 2, 3, 4])

pandas/sparse/tests/test_array.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from pandas import _np_version_under1p8
10-
from pandas.sparse.api import SparseArray
10+
from pandas.sparse.api import SparseArray, SparseSeries
1111
from pandas._sparse import IntIndex
1212
from pandas.util.testing import assert_almost_equal, assertRaisesRegexp
1313
import pandas.util.testing as tm
@@ -102,6 +102,32 @@ def test_constructor_spindex_dtype(self):
102102
self.assertEqual(arr.dtype, np.int64)
103103
self.assertEqual(arr.fill_value, 0)
104104

105+
def test_sparseseries_roundtrip(self):
106+
# GH 13999
107+
for kind in ['integer', 'block']:
108+
for fill in [1, np.nan, 0]:
109+
arr = SparseArray([np.nan, 1, np.nan, 2, 3], kind=kind,
110+
fill_value=fill)
111+
res = SparseArray(SparseSeries(arr))
112+
tm.assert_sp_array_equal(arr, res)
113+
114+
arr = SparseArray([0, 0, 0, 1, 1, 2], dtype=np.int64,
115+
kind=kind, fill_value=fill)
116+
res = SparseArray(SparseSeries(arr), dtype=np.int64)
117+
tm.assert_sp_array_equal(arr, res)
118+
119+
res = SparseArray(SparseSeries(arr))
120+
tm.assert_sp_array_equal(arr, res)
121+
122+
for fill in [True, False, np.nan]:
123+
arr = SparseArray([True, False, True, True], dtype=np.bool,
124+
kind=kind, fill_value=fill)
125+
res = SparseArray(SparseSeries(arr))
126+
tm.assert_sp_array_equal(arr, res)
127+
128+
res = SparseArray(SparseSeries(arr))
129+
tm.assert_sp_array_equal(arr, res)
130+
105131
def test_get_item(self):
106132

107133
self.assertTrue(np.isnan(self.arr[1]))

0 commit comments

Comments
 (0)