diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 581106924c77e..03e0cae6cc83f 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -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`) diff --git a/pandas/sparse/array.py b/pandas/sparse/array.py index 8420371d05e02..a15def65cad7e 100644 --- a/pandas/sparse/array.py +++ b/pandas/sparse/array.py @@ -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 @@ -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): diff --git a/pandas/sparse/series.py b/pandas/sparse/series.py index ad9168890b8f2..660f76ff1001d 100644 --- a/pandas/sparse/series.py +++ b/pandas/sparse/series.py @@ -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) diff --git a/pandas/sparse/tests/test_array.py b/pandas/sparse/tests/test_array.py index 2b284ac631d3f..1c9b6119cf665 100644 --- a/pandas/sparse/tests/test_array.py +++ b/pandas/sparse/tests/test_array.py @@ -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]) diff --git a/pandas/sparse/tests/test_series.py b/pandas/sparse/tests/test_series.py index de8c63df9c9e6..116596e36b402 100644 --- a/pandas/sparse/tests/test_series.py +++ b/pandas/sparse/tests/test_series.py @@ -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')