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 Oct 18, 2018
1 parent 1546c35 commit 2d1cebf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 61 deletions.
7 changes: 5 additions & 2 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,15 @@ In addition to these API breaking changes, many :ref:`performance improvements a
Raise ValueError in ``DataFrame.to_dict(orient='index')``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

.. ipython:: python
:okexcept:

df = pd.DataFrame({'a': [1, 2], 'b': [0.5, 0.75]}, index=['A', 'A'])
df

df.to_dict(orient='index')

.. _whatsnew_0240.api.datetimelike.normalize:
Expand Down Expand Up @@ -730,6 +730,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` 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`)
- :meth:`SparseArray.get_values` has deprecated the ``fill`` parameter (:issue:`14686`)

.. _whatsnew_0240.prior_deprecations:

Expand All @@ -747,6 +748,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
17 changes: 11 additions & 6 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,13 +1250,8 @@ def map(self, mapper):
fill_value=fill_value)

def get_values(self, fill=None):
""" return a dense representation """
# TODO: deprecate for to_dense?
return self.to_dense(fill=fill)

def to_dense(self, fill=None):
"""
Convert SparseArray to a NumPy array.
Return a dense representation.
Parameters
----------
Expand All @@ -1272,6 +1267,16 @@ def to_dense(self, fill=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 np.asarray(self, dtype=self.sp_values.dtype)

# ------------------------------------------------------------------------
Expand Down
23 changes: 3 additions & 20 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,33 +439,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
43 changes: 19 additions & 24 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,32 +533,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
9 changes: 0 additions & 9 deletions pandas/tests/sparse/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,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

0 comments on commit 2d1cebf

Please sign in to comment.