Skip to content

Commit

Permalink
BUG: Patch to_dense behaviour for sparse.
Browse files Browse the repository at this point in the history
Patches the following for to_dense:

1) Fix SparseArray.to_dense documentation
to refer to SparseArray and not SparseSeries.

2) Deprecate the fill parameter in
SparseArray.to_dense, as that parameter was
not being respected.

3) Deprecate the sparse_only parameter in
SparseSeries.to_dense, as that parameter is
inconsistent with the to_dense API we want,
which is no parameters.

Closes gh-14647.
  • Loading branch information
gfyoung committed Nov 22, 2016
1 parent 3443de7 commit 0c2f535
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Removal of prior version deprecations/changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- ``pd.to_datetime`` and ``pd.to_timedelta`` have dropped the ``coerce`` parameter in favor of ``errors`` (:issue:`13602`)
- ``SparseArray.to_dense()`` has deprecated the ``fill`` parameter, as that parameter was not being respected (:issue:`14647`)
- ``SparseSeries.to_dense()`` has deprecated the ``sparse_only`` parameter (:issue:`14647`)



Expand Down
17 changes: 16 additions & 1 deletion pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# pylint: disable=E1101,E1103,W0231

import numpy as np
import warnings

import pandas as pd
from pandas.core.base import PandasObject
Expand Down Expand Up @@ -381,8 +382,22 @@ def get_values(self, fill=None):

def to_dense(self, fill=None):
"""
Convert SparseSeries to (dense) Series
Convert SparseArray to a NumPy array.
Parameters
----------
fill: float, default None
DEPRECATED: this argument will be removed in a future version
because it is not respected by this function.
Returns
-------
arr : NumPy array
"""
if fill is not None:
warnings.warn(("The 'fill' parameter has been deprecated and "
"will be removed in a future version."),
FutureWarning, stacklevel=2)
return self.values

def __iter__(self):
Expand Down
17 changes: 16 additions & 1 deletion pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,24 @@ def _set_values(self, key, value):

def to_dense(self, sparse_only=False):
"""
Convert SparseSeries to (dense) Series
Convert SparseSeries to a Series.
Parameters
----------
sparse_only: bool, default False
DEPRECATED: this argument will be removed in a future version.
If True, return just the non-sparse values, or the dense version
of `self.values` if False.
Returns
-------
s : Series
"""
if sparse_only:
warnings.warn(("The 'sparse_only' parameter has been deprecated "
"and will be removed in a future version."),
FutureWarning, stacklevel=2)
int_index = self.sp_index.to_int_index()
index = self.index.take(int_index.indices)
return Series(self.sp_values, index=index, name=self.name)
Expand Down
5 changes: 5 additions & 0 deletions pandas/sparse/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ def test_to_dense(self):
res = SparseArray(vals, fill_value=0).to_dense()
tm.assert_numpy_array_equal(res, vals)

# see gh-14647
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
SparseArray(vals).to_dense(fill=2)

def test_getitem(self):
def _checkit(i):
assert_almost_equal(self.arr[i], self.arr.values[i])
Expand Down
5 changes: 4 additions & 1 deletion pandas/sparse/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def test_sparse_to_dense(self):
series = self.bseries.to_dense()
tm.assert_series_equal(series, Series(arr, name='bseries'))

series = self.bseries.to_dense(sparse_only=True)
# see gh-14647
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
series = self.bseries.to_dense(sparse_only=True)

indexer = np.isfinite(arr)
exp = Series(arr[indexer], index=index[indexer], name='bseries')
Expand Down

0 comments on commit 0c2f535

Please sign in to comment.