Skip to content

Commit

Permalink
DEPR/CLN: Clean up to_dense signature deprecations
Browse files Browse the repository at this point in the history
Also deprecate `fill` in `.get_values` because its
parameter was being passed to `.to_dense`, which
no longer accepts the `fill` parameter.

xref pandas-devgh-14686.
  • Loading branch information
gfyoung committed Sep 30, 2018
1 parent f4fae35 commit 21de4f4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 63 deletions.
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.24.0.txt
Expand Up @@ -574,6 +574,7 @@ Deprecations
many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`)
- :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`)
- :func:`DatetimeIndex.shift` now accepts ``periods`` argument instead of ``n`` for consistency with :func:`Index.shift` and :func:`Series.shift`. Using ``n`` throws a deprecation warning (:issue:`22458`)
- :meth:`SparseArray.get_values` has deprecated the ``fill`` parameter (:issue:`14686`)

.. _whatsnew_0240.prior_deprecations:

Expand All @@ -591,6 +592,8 @@ Removal of prior version deprecations/changes
- :meth:`Categorical.searchsorted` and :meth:`Series.searchsorted` have renamed the ``v`` argument to ``value`` (:issue:`14645`)
- :meth:`TimedeltaIndex.searchsorted`, :meth:`DatetimeIndex.searchsorted`, and :meth:`PeriodIndex.searchsorted` have renamed the ``key`` argument to ``value`` (:issue:`14645`)
- Removal of the previously deprecated module ``pandas.json`` (:issue:`19944`)
- :meth:`SparseArray.to_dense` has dropped the ``fill`` parameter (:issue:`14686`)
- :meth:`SparseSeries.to_dense` has dropped the ``sparse_only`` parameter (:issue:`14686`)

.. _whatsnew_0240.performance:

Expand Down
22 changes: 12 additions & 10 deletions pandas/core/sparse/array.py
Expand Up @@ -366,27 +366,29 @@ def fill_value(self, value):
raise ValueError(msg.format(fill=value, dtype=self.dtype))

def get_values(self, fill=None):
""" return a dense representation """
return self.to_dense(fill=fill)

def to_dense(self, fill=None):
"""
Convert SparseArray to a NumPy array.
Return a dense representation.
Parameters
----------
fill: float, default None
.. deprecated:: 0.20.0
.. deprecated:: 0.24.0
This argument 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.to_dense()

def to_dense(self):
"""
Convert SparseArray to a NumPy array.
Returns
-------
arr : NumPy array
"""
return self.values

def __iter__(self):
Expand Down
23 changes: 3 additions & 20 deletions pandas/core/sparse/series.py
Expand Up @@ -518,33 +518,16 @@ def _set_values(self, key, value):
kind=self.kind)
self._data = SingleBlockManager(values, self.index)

def to_dense(self, sparse_only=False):
def to_dense(self):
"""
Convert SparseSeries to a Series.
Parameters
----------
sparse_only : bool, default False
.. deprecated:: 0.20.0
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)
else:
return Series(self.values.to_dense(), index=self.index,
name=self.name)
return Series(self.values.to_dense(), index=self.index,
name=self.name)

@property
def density(self):
Expand Down
9 changes: 0 additions & 9 deletions pandas/tests/sparse/series/test_series.py
Expand Up @@ -191,15 +191,6 @@ def test_sparse_to_dense(self):
series = self.bseries.to_dense()
tm.assert_series_equal(series, Series(arr, name='bseries'))

# 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')
tm.assert_series_equal(series, exp)

series = self.iseries.to_dense()
tm.assert_series_equal(series, Series(arr, name='iseries'))

Expand Down
43 changes: 19 additions & 24 deletions pandas/tests/sparse/test_array.py
Expand Up @@ -471,32 +471,27 @@ def test_shape(self, data, shape, dtype):
out = SparseArray(data, dtype=dtype)
assert out.shape == shape

def test_to_dense(self):
vals = np.array([1, np.nan, np.nan, 3, np.nan])
res = SparseArray(vals).to_dense()
tm.assert_numpy_array_equal(res, vals)

res = SparseArray(vals, fill_value=0).to_dense()
tm.assert_numpy_array_equal(res, vals)

vals = np.array([1, np.nan, 0, 3, 0])
res = SparseArray(vals).to_dense()
tm.assert_numpy_array_equal(res, vals)

res = SparseArray(vals, fill_value=0).to_dense()
tm.assert_numpy_array_equal(res, vals)

vals = np.array([np.nan, np.nan, np.nan, np.nan, np.nan])
res = SparseArray(vals).to_dense()
tm.assert_numpy_array_equal(res, vals)

res = SparseArray(vals, fill_value=0).to_dense()
@pytest.mark.parametrize("vals", [
[np.nan, np.nan, np.nan, np.nan, np.nan],
[1, np.nan, np.nan, 3, np.nan],
[1, np.nan, 0, 3, 0],
])
@pytest.mark.parametrize("method", ["to_dense", "get_values"])
@pytest.mark.parametrize("fill_value", [None, 0])
def test_dense_repr(self, vals, fill_value, method):
vals = np.array(vals)
arr = SparseArray(vals, fill_value=fill_value)
dense_func = getattr(arr, method)

res = dense_func()
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)
if method == "get_values":
# see gh-14686
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
res = dense_func(fill=0)
tm.assert_numpy_array_equal(res, vals)

def test_getitem(self):
def _checkit(i):
Expand Down

0 comments on commit 21de4f4

Please sign in to comment.