Skip to content

Commit 328d971

Browse files
gfyoungtm9k1
authored andcommitted
DEPR/CLN: Clean up to_dense signature deprecations (pandas-dev#22910)
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 358bf13 commit 328d971

File tree

5 files changed

+22
-70
lines changed

5 files changed

+22
-70
lines changed

doc/source/whatsnew/v0.24.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ Removal of prior version deprecations/changes
752752
- :meth:`Categorical.searchsorted` and :meth:`Series.searchsorted` have renamed the ``v`` argument to ``value`` (:issue:`14645`)
753753
- :meth:`TimedeltaIndex.searchsorted`, :meth:`DatetimeIndex.searchsorted`, and :meth:`PeriodIndex.searchsorted` have renamed the ``key`` argument to ``value`` (:issue:`14645`)
754754
- Removal of the previously deprecated module ``pandas.json`` (:issue:`19944`)
755+
- :meth:`SparseArray.get_values` and :meth:`SparseArray.to_dense` have dropped the ``fill`` parameter (:issue:`14686`)
756+
- :meth:`SparseSeries.to_dense` has dropped the ``sparse_only`` parameter (:issue:`14686`)
755757

756758
.. _whatsnew_0240.performance:
757759

pandas/core/arrays/sparse.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -1249,31 +1249,19 @@ def map(self, mapper):
12491249
return type(self)(sp_values, sparse_index=self.sp_index,
12501250
fill_value=fill_value)
12511251

1252-
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):
1252+
def to_dense(self):
12581253
"""
12591254
Convert SparseArray to a NumPy array.
12601255
1261-
Parameters
1262-
----------
1263-
fill: float, default None
1264-
.. deprecated:: 0.20.0
1265-
This argument is not respected by this function.
1266-
12671256
Returns
12681257
-------
12691258
arr : NumPy array
12701259
"""
1271-
if fill is not None:
1272-
warnings.warn(("The 'fill' parameter has been deprecated and "
1273-
"will be removed in a future version."),
1274-
FutureWarning, stacklevel=2)
12751260
return np.asarray(self, dtype=self.sp_values.dtype)
12761261

1262+
# TODO: Look into deprecating this in favor of `to_dense`.
1263+
get_values = to_dense
1264+
12771265
# ------------------------------------------------------------------------
12781266
# IO
12791267
# ------------------------------------------------------------------------

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

+13-25
Original file line numberDiff line numberDiff line change
@@ -533,33 +533,21 @@ 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)
562-
563551
def test_getitem(self):
564552
def _checkit(i):
565553
assert_almost_equal(self.arr[i], self.arr.values[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)