Skip to content

Commit 2d1cebf

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 gh-14686.
1 parent 1546c35 commit 2d1cebf

File tree

5 files changed

+38
-61
lines changed

5 files changed

+38
-61
lines changed

doc/source/whatsnew/v0.24.0.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,15 @@ In addition to these API breaking changes, many :ref:`performance improvements a
440440
Raise ValueError in ``DataFrame.to_dict(orient='index')``
441441
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
442442

443-
Bug in :func:`DataFrame.to_dict` raises ``ValueError`` when used with
443+
Bug in :func:`DataFrame.to_dict` raises ``ValueError`` when used with
444444
``orient='index'`` and a non-unique index instead of losing data (:issue:`22801`)
445445

446446
.. ipython:: python
447447
:okexcept:
448448

449449
df = pd.DataFrame({'a': [1, 2], 'b': [0.5, 0.75]}, index=['A', 'A'])
450450
df
451-
451+
452452
df.to_dict(orient='index')
453453

454454
.. _whatsnew_0240.api.datetimelike.normalize:
@@ -730,6 +730,7 @@ Deprecations
730730
many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`)
731731
- :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`)
732732
- :func:`DatetimeIndex.shift` and :func:`PeriodIndex.shift` now accept ``periods`` argument instead of ``n`` for consistency with :func:`Index.shift` and :func:`Series.shift`. Using ``n`` throws a deprecation warning (:issue:`22458`, :issue:`22912`)
733+
- :meth:`SparseArray.get_values` has deprecated the ``fill`` parameter (:issue:`14686`)
733734

734735
.. _whatsnew_0240.prior_deprecations:
735736

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

751754
.. _whatsnew_0240.performance:
752755

pandas/core/arrays/sparse.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1250,13 +1250,8 @@ def map(self, mapper):
12501250
fill_value=fill_value)
12511251

12521252
def get_values(self, fill=None):
1253-
""" return a dense representation """
1254-
# TODO: deprecate for to_dense?
1255-
return self.to_dense(fill=fill)
1256-
1257-
def to_dense(self, fill=None):
12581253
"""
1259-
Convert SparseArray to a NumPy array.
1254+
Return a dense representation.
12601255
12611256
Parameters
12621257
----------
@@ -1272,6 +1267,16 @@ def to_dense(self, fill=None):
12721267
warnings.warn(("The 'fill' parameter has been deprecated and "
12731268
"will be removed in a future version."),
12741269
FutureWarning, stacklevel=2)
1270+
return self.to_dense()
1271+
1272+
def to_dense(self):
1273+
"""
1274+
Convert SparseArray to a NumPy array.
1275+
1276+
Returns
1277+
-------
1278+
arr : NumPy array
1279+
"""
12751280
return np.asarray(self, dtype=self.sp_values.dtype)
12761281

12771282
# ------------------------------------------------------------------------

pandas/core/sparse/series.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -439,33 +439,16 @@ def _set_values(self, key, value):
439439
kind=self.kind)
440440
self._data = SingleBlockManager(values, self.index)
441441

442-
def to_dense(self, sparse_only=False):
442+
def to_dense(self):
443443
"""
444444
Convert SparseSeries to a Series.
445445
446-
Parameters
447-
----------
448-
sparse_only : bool, default False
449-
.. deprecated:: 0.20.0
450-
This argument will be removed in a future version.
451-
452-
If True, return just the non-sparse values, or the dense version
453-
of `self.values` if False.
454-
455446
Returns
456447
-------
457448
s : Series
458449
"""
459-
if sparse_only:
460-
warnings.warn(("The 'sparse_only' parameter has been deprecated "
461-
"and will be removed in a future version."),
462-
FutureWarning, stacklevel=2)
463-
int_index = self.sp_index.to_int_index()
464-
index = self.index.take(int_index.indices)
465-
return Series(self.sp_values, index=index, name=self.name)
466-
else:
467-
return Series(self.values.to_dense(), index=self.index,
468-
name=self.name)
450+
return Series(self.values.to_dense(), index=self.index,
451+
name=self.name)
469452

470453
@property
471454
def density(self):

pandas/tests/arrays/sparse/test_array.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -533,32 +533,27 @@ def test_shape(self, data, shape, dtype):
533533
out = SparseArray(data, dtype=dtype)
534534
assert out.shape == shape
535535

536-
def test_to_dense(self):
537-
vals = np.array([1, np.nan, np.nan, 3, np.nan])
538-
res = SparseArray(vals).to_dense()
539-
tm.assert_numpy_array_equal(res, vals)
540-
541-
res = SparseArray(vals, fill_value=0).to_dense()
542-
tm.assert_numpy_array_equal(res, vals)
543-
544-
vals = np.array([1, np.nan, 0, 3, 0])
545-
res = SparseArray(vals).to_dense()
546-
tm.assert_numpy_array_equal(res, vals)
547-
548-
res = SparseArray(vals, fill_value=0).to_dense()
549-
tm.assert_numpy_array_equal(res, vals)
550-
551-
vals = np.array([np.nan, np.nan, np.nan, np.nan, np.nan])
552-
res = SparseArray(vals).to_dense()
553-
tm.assert_numpy_array_equal(res, vals)
554-
555-
res = SparseArray(vals, fill_value=0).to_dense()
536+
@pytest.mark.parametrize("vals", [
537+
[np.nan, np.nan, np.nan, np.nan, np.nan],
538+
[1, np.nan, np.nan, 3, np.nan],
539+
[1, np.nan, 0, 3, 0],
540+
])
541+
@pytest.mark.parametrize("method", ["to_dense", "get_values"])
542+
@pytest.mark.parametrize("fill_value", [None, 0])
543+
def test_dense_repr(self, vals, fill_value, method):
544+
vals = np.array(vals)
545+
arr = SparseArray(vals, fill_value=fill_value)
546+
dense_func = getattr(arr, method)
547+
548+
res = dense_func()
556549
tm.assert_numpy_array_equal(res, vals)
557550

558-
# see gh-14647
559-
with tm.assert_produces_warning(FutureWarning,
560-
check_stacklevel=False):
561-
SparseArray(vals).to_dense(fill=2)
551+
if method == "get_values":
552+
# see gh-14686
553+
with tm.assert_produces_warning(FutureWarning,
554+
check_stacklevel=False):
555+
res = dense_func(fill=0)
556+
tm.assert_numpy_array_equal(res, vals)
562557

563558
def test_getitem(self):
564559
def _checkit(i):

pandas/tests/sparse/series/test_series.py

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

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

0 commit comments

Comments
 (0)