Skip to content

Commit 21de4f4

Browse files
committed
DEPR/CLN: Clean up to_dense signature deprecations
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.
1 parent f4fae35 commit 21de4f4

File tree

5 files changed

+37
-63
lines changed

5 files changed

+37
-63
lines changed

doc/source/whatsnew/v0.24.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ Deprecations
574574
many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`)
575575
- :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`)
576576
- :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`)
577+
- :meth:`SparseArray.get_values` has deprecated the ``fill`` parameter (:issue:`14686`)
577578

578579
.. _whatsnew_0240.prior_deprecations:
579580

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

595598
.. _whatsnew_0240.performance:
596599

pandas/core/sparse/array.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,29 @@ def fill_value(self, value):
366366
raise ValueError(msg.format(fill=value, dtype=self.dtype))
367367

368368
def get_values(self, fill=None):
369-
""" return a dense representation """
370-
return self.to_dense(fill=fill)
371-
372-
def to_dense(self, fill=None):
373369
"""
374-
Convert SparseArray to a NumPy array.
370+
Return a dense representation.
375371
376372
Parameters
377373
----------
378374
fill: float, default None
379-
.. deprecated:: 0.20.0
375+
.. deprecated:: 0.24.0
380376
This argument is not respected by this function.
381-
382-
Returns
383-
-------
384-
arr : NumPy array
385377
"""
386378
if fill is not None:
387379
warnings.warn(("The 'fill' parameter has been deprecated and "
388380
"will be removed in a future version."),
389381
FutureWarning, stacklevel=2)
382+
return self.to_dense()
383+
384+
def to_dense(self):
385+
"""
386+
Convert SparseArray to a NumPy array.
387+
388+
Returns
389+
-------
390+
arr : NumPy array
391+
"""
390392
return self.values
391393

392394
def __iter__(self):

pandas/core/sparse/series.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -518,33 +518,16 @@ def _set_values(self, key, value):
518518
kind=self.kind)
519519
self._data = SingleBlockManager(values, self.index)
520520

521-
def to_dense(self, sparse_only=False):
521+
def to_dense(self):
522522
"""
523523
Convert SparseSeries to a Series.
524524
525-
Parameters
526-
----------
527-
sparse_only : bool, default False
528-
.. deprecated:: 0.20.0
529-
This argument will be removed in a future version.
530-
531-
If True, return just the non-sparse values, or the dense version
532-
of `self.values` if False.
533-
534525
Returns
535526
-------
536527
s : Series
537528
"""
538-
if sparse_only:
539-
warnings.warn(("The 'sparse_only' parameter has been deprecated "
540-
"and will be removed in a future version."),
541-
FutureWarning, stacklevel=2)
542-
int_index = self.sp_index.to_int_index()
543-
index = self.index.take(int_index.indices)
544-
return Series(self.sp_values, index=index, name=self.name)
545-
else:
546-
return Series(self.values.to_dense(), index=self.index,
547-
name=self.name)
529+
return Series(self.values.to_dense(), index=self.index,
530+
name=self.name)
548531

549532
@property
550533
def density(self):

pandas/tests/sparse/series/test_series.py

-9
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,6 @@ def test_sparse_to_dense(self):
191191
series = self.bseries.to_dense()
192192
tm.assert_series_equal(series, Series(arr, name='bseries'))
193193

194-
# see gh-14647
195-
with tm.assert_produces_warning(FutureWarning,
196-
check_stacklevel=False):
197-
series = self.bseries.to_dense(sparse_only=True)
198-
199-
indexer = np.isfinite(arr)
200-
exp = Series(arr[indexer], index=index[indexer], name='bseries')
201-
tm.assert_series_equal(series, exp)
202-
203194
series = self.iseries.to_dense()
204195
tm.assert_series_equal(series, Series(arr, name='iseries'))
205196

pandas/tests/sparse/test_array.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -471,32 +471,27 @@ def test_shape(self, data, shape, dtype):
471471
out = SparseArray(data, dtype=dtype)
472472
assert out.shape == shape
473473

474-
def test_to_dense(self):
475-
vals = np.array([1, np.nan, np.nan, 3, np.nan])
476-
res = SparseArray(vals).to_dense()
477-
tm.assert_numpy_array_equal(res, vals)
478-
479-
res = SparseArray(vals, fill_value=0).to_dense()
480-
tm.assert_numpy_array_equal(res, vals)
481-
482-
vals = np.array([1, np.nan, 0, 3, 0])
483-
res = SparseArray(vals).to_dense()
484-
tm.assert_numpy_array_equal(res, vals)
485-
486-
res = SparseArray(vals, fill_value=0).to_dense()
487-
tm.assert_numpy_array_equal(res, vals)
488-
489-
vals = np.array([np.nan, np.nan, np.nan, np.nan, np.nan])
490-
res = SparseArray(vals).to_dense()
491-
tm.assert_numpy_array_equal(res, vals)
492-
493-
res = SparseArray(vals, fill_value=0).to_dense()
474+
@pytest.mark.parametrize("vals", [
475+
[np.nan, np.nan, np.nan, np.nan, np.nan],
476+
[1, np.nan, np.nan, 3, np.nan],
477+
[1, np.nan, 0, 3, 0],
478+
])
479+
@pytest.mark.parametrize("method", ["to_dense", "get_values"])
480+
@pytest.mark.parametrize("fill_value", [None, 0])
481+
def test_dense_repr(self, vals, fill_value, method):
482+
vals = np.array(vals)
483+
arr = SparseArray(vals, fill_value=fill_value)
484+
dense_func = getattr(arr, method)
485+
486+
res = dense_func()
494487
tm.assert_numpy_array_equal(res, vals)
495488

496-
# see gh-14647
497-
with tm.assert_produces_warning(FutureWarning,
498-
check_stacklevel=False):
499-
SparseArray(vals).to_dense(fill=2)
489+
if method == "get_values":
490+
# see gh-14686
491+
with tm.assert_produces_warning(FutureWarning,
492+
check_stacklevel=False):
493+
res = dense_func(fill=0)
494+
tm.assert_numpy_array_equal(res, vals)
500495

501496
def test_getitem(self):
502497
def _checkit(i):

0 commit comments

Comments
 (0)