Skip to content

DEPR/CLN: Clean up to_dense signature deprecations #22910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 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 @@ -747,6 +747,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.get_values` and :meth:`SparseArray.to_dense` have dropped the ``fill`` parameter (:issue:`14686`)
- :meth:`SparseSeries.to_dense` has dropped the ``sparse_only`` parameter (:issue:`14686`)

.. _whatsnew_0240.performance:

Expand Down
20 changes: 4 additions & 16 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,31 +1249,19 @@ def map(self, mapper):
return type(self)(sp_values, sparse_index=self.sp_index,
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):
def to_dense(self):
"""
Convert SparseArray to a NumPy array.

Parameters
----------
fill: float, default None
.. deprecated:: 0.20.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 np.asarray(self, dtype=self.sp_values.dtype)

# TODO: Look into deprecating this in favor of `to_dense`.
get_values = to_dense

# ------------------------------------------------------------------------
# IO
# ------------------------------------------------------------------------
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
38 changes: 13 additions & 25 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,33 +533,21 @@ 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)

def test_getitem(self):
def _checkit(i):
assert_almost_equal(self.arr[i], self.arr.values[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