Skip to content

Commit 867fe23

Browse files
committed
BUG: Patch to_dense behaviour for sparse.
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 pandas-devgh-14647.
1 parent fdb70a9 commit 867fe23

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Removal of prior version deprecations/changes
6464
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6565

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

6870

6971

pandas/sparse/array.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# pylint: disable=E1101,E1103,W0231
66

77
import numpy as np
8+
import warnings
89

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

382383
def to_dense(self, fill=None):
383384
"""
384-
Convert SparseSeries to (dense) Series
385+
Convert SparseArray to a NumPy array.
386+
387+
Parameters
388+
----------
389+
fill: float, default None
390+
DEPRECATED: this argument will be removed in a future version
391+
because it is not respected by this function.
392+
393+
Returns
394+
-------
395+
arr : NumPy array
385396
"""
397+
if fill is not None:
398+
warnings.warn(("The 'fill' parameter has been deprecated and "
399+
"will be removed in a future version."),
400+
FutureWarning, stacklevel=2)
386401
return self.values
387402

388403
def __iter__(self):

pandas/sparse/series.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,24 @@ def _set_values(self, key, value):
528528

529529
def to_dense(self, sparse_only=False):
530530
"""
531-
Convert SparseSeries to (dense) Series
531+
Convert SparseSeries to a Series.
532+
533+
Parameters
534+
----------
535+
sparse_only: bool, default False
536+
DEPRECATED: this argument will be removed in a future version.
537+
538+
If True, return just the non-sparse values, or the dense version
539+
of `self.values` if False.
540+
541+
Returns
542+
-------
543+
s : Series
532544
"""
533545
if sparse_only:
546+
warnings.warn(("The 'sparse_only' parameter has been deprecated "
547+
"and will be removed in a future version."),
548+
FutureWarning, stacklevel=2)
534549
int_index = self.sp_index.to_int_index()
535550
index = self.index.take(int_index.indices)
536551
return Series(self.sp_values, index=index, name=self.name)

pandas/sparse/tests/test_array.py

+5
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ def test_to_dense(self):
453453
res = SparseArray(vals, fill_value=0).to_dense()
454454
tm.assert_numpy_array_equal(res, vals)
455455

456+
# see gh-14647
457+
with tm.assert_produces_warning(FutureWarning,
458+
check_stacklevel=False):
459+
SparseArray(vals).to_dense(fill=2)
460+
456461
def test_getitem(self):
457462
def _checkit(i):
458463
assert_almost_equal(self.arr[i], self.arr.values[i])

pandas/sparse/tests/test_series.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ def test_sparse_to_dense(self):
161161
series = self.bseries.to_dense()
162162
tm.assert_series_equal(series, Series(arr, name='bseries'))
163163

164-
series = self.bseries.to_dense(sparse_only=True)
164+
# see gh-14647
165+
with tm.assert_produces_warning(FutureWarning,
166+
check_stacklevel=False):
167+
series = self.bseries.to_dense(sparse_only=True)
165168

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

0 commit comments

Comments
 (0)