Skip to content

Commit

Permalink
BUG: fix SparseSeries reindex by using Series implementation
Browse files Browse the repository at this point in the history
closes pandas-dev#15447

Author: Pietro Battiston <me@pietrobattiston.it>

Closes pandas-dev#15461 from toobaz/drop_sparse_reindex and squashes the following commits:

9084246 [Pietro Battiston] Test SparseSeries.reindex with fill_value and nearest
d6a46da [Pietro Battiston] Use _shared_docs for documentation
922c7b0 [Pietro Battiston] Test "copy" argument
af99190 [Pietro Battiston] Whatsnew
7945cb4 [Pietro Battiston] Tests for .loc() and .reindex() on sparse series with MultiIndex
55b99f8 [Pietro Battiston] BUG: Drop faulty and redundant reindex() for SparseSeries
  • Loading branch information
toobaz authored and AnkurDedania committed Mar 21, 2017
1 parent f5fc387 commit 9ee6980
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ Bug Fixes
- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)


- Bug in ``SparseSeries.reindex`` on single level with list of length 1 (:issue:`15447`)



- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)

- Bug in ``.asfreq()``, where frequency was not set for empty ``Series` (:issue:`14320`)
Expand Down
24 changes: 5 additions & 19 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
_coo_to_sparse_series)


_shared_doc_kwargs = dict(klass='SparseSeries',
_shared_doc_kwargs = dict(axes='index', klass='SparseSeries',
axes_single_arg="{0, 'index'}")

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -570,27 +570,13 @@ def copy(self, deep=True):
return self._constructor(new_data, sparse_index=self.sp_index,
fill_value=self.fill_value).__finalize__(self)

@Appender(generic._shared_docs['reindex'] % _shared_doc_kwargs)
def reindex(self, index=None, method=None, copy=True, limit=None,
**kwargs):
"""
Conform SparseSeries to new Index
See Series.reindex docstring for general behavior

Returns
-------
reindexed : SparseSeries
"""
new_index = _ensure_index(index)

if self.index.equals(new_index):
if copy:
return self.copy()
else:
return self
return self._constructor(self._data.reindex(new_index, method=method,
limit=limit, copy=copy),
index=new_index).__finalize__(self)
return super(SparseSeries, self).reindex(index=index, method=method,
copy=copy, limit=limit,
**kwargs)

def sparse_reindex(self, new_index):
"""
Expand Down
53 changes: 52 additions & 1 deletion pandas/tests/sparse/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def test_reindex(self):
exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse()
tm.assert_sp_series_equal(res, exp)

def test_reindex_fill_value(self):
def test_fill_value_reindex(self):
orig = pd.Series([1, np.nan, 0, 3, 0], index=list('ABCDE'))
sparse = orig.to_sparse(fill_value=0)

Expand Down Expand Up @@ -397,6 +397,23 @@ def test_reindex_fill_value(self):
exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse(fill_value=0)
tm.assert_sp_series_equal(res, exp)

def test_reindex_fill_value(self):
floats = pd.Series([1., 2., 3.]).to_sparse()
result = floats.reindex([1, 2, 3], fill_value=0)
expected = pd.Series([2., 3., 0], index=[1, 2, 3]).to_sparse()
tm.assert_sp_series_equal(result, expected)

def test_reindex_nearest(self):
s = pd.Series(np.arange(10, dtype='float64')).to_sparse()
target = [0.1, 0.9, 1.5, 2.0]
actual = s.reindex(target, method='nearest')
expected = pd.Series(np.around(target), target).to_sparse()
tm.assert_sp_series_equal(expected, actual)

actual = s.reindex(target, method='nearest', tolerance=0.2)
expected = pd.Series([0, 1, np.nan, 2], target).to_sparse()
tm.assert_sp_series_equal(expected, actual)

def tests_indexing_with_sparse(self):
# GH 13985

Expand Down Expand Up @@ -504,6 +521,11 @@ def test_loc(self):
exp = orig.loc[[1, 3, 4, 5]].to_sparse()
tm.assert_sp_series_equal(result, exp)

# single element list (GH 15447)
result = sparse.loc[['A']]
exp = orig.loc[['A']].to_sparse()
tm.assert_sp_series_equal(result, exp)

# dense array
result = sparse.loc[orig % 2 == 1]
exp = orig.loc[orig % 2 == 1].to_sparse()
Expand Down Expand Up @@ -537,6 +559,35 @@ def test_loc_slice(self):
orig.loc['A':'B'].to_sparse())
tm.assert_sp_series_equal(sparse.loc[:'B'], orig.loc[:'B'].to_sparse())

def test_reindex(self):
# GH 15447
orig = self.orig
sparse = self.sparse

res = sparse.reindex([('A', 0), ('C', 1)])
exp = orig.reindex([('A', 0), ('C', 1)]).to_sparse()
tm.assert_sp_series_equal(res, exp)

# On specific level:
res = sparse.reindex(['A', 'C', 'B'], level=0)
exp = orig.reindex(['A', 'C', 'B'], level=0).to_sparse()
tm.assert_sp_series_equal(res, exp)

# single element list (GH 15447)
res = sparse.reindex(['A'], level=0)
exp = orig.reindex(['A'], level=0).to_sparse()
tm.assert_sp_series_equal(res, exp)

with tm.assertRaises(TypeError):
# Incomplete keys are not accepted for reindexing:
sparse.reindex(['A', 'C'])

# "copy" argument:
res = sparse.reindex(sparse.index, copy=True)
exp = orig.reindex(orig.index, copy=True).to_sparse()
tm.assert_sp_series_equal(res, exp)
self.assertIsNot(sparse, res)


class TestSparseDataFrameIndexing(tm.TestCase):

Expand Down

0 comments on commit 9ee6980

Please sign in to comment.