Skip to content

Commit 376a05e

Browse files
VikramjeetDjreback
authored andcommitted
DEPR: Deprecate Series/Dataframe.to_dense/to_sparse (#26684)
1 parent 9ceb029 commit 376a05e

23 files changed

+117
-15
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ Other Deprecations
503503
- The :meth:`Series.ftype`, :meth:`Series.ftypes` and :meth:`DataFrame.ftypes` methods are deprecated and will be removed in a future version.
504504
Instead, use :meth:`Series.dtype` and :meth:`DataFrame.dtypes` (:issue:`26705`).
505505
- :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.resolution_string`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`)
506+
- :meth:`Series.to_sparse`, :meth:`DataFrame.to_sparse`, :meth:`Series.to_dense` and :meth:`DataFrame.to_dense` are deprecated and will be removed in a future version. (:issue:`26557`).
506507

507508
.. _whatsnew_0250.prior_deprecations:
508509

pandas/core/frame.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,8 @@ def to_sparse(self, fill_value=None, kind='block'):
18891889
"""
18901890
Convert to SparseDataFrame.
18911891
1892+
.. deprecated:: 0.25.0
1893+
18921894
Implement the sparse version of the DataFrame meaning that any data
18931895
matching a specific value it's omitted in the representation.
18941896
The sparse DataFrame allows for a more efficient storage.
@@ -1939,10 +1941,15 @@ def to_sparse(self, fill_value=None, kind='block'):
19391941
>>> type(sdf) # doctest: +SKIP
19401942
<class 'pandas.core.sparse.frame.SparseDataFrame'>
19411943
"""
1944+
warnings.warn("DataFrame.to_sparse is deprecated and will be removed "
1945+
"in a future version", FutureWarning, stacklevel=2)
1946+
19421947
from pandas.core.sparse.api import SparseDataFrame
1943-
return SparseDataFrame(self._series, index=self.index,
1944-
columns=self.columns, default_kind=kind,
1945-
default_fill_value=fill_value)
1948+
with warnings.catch_warnings():
1949+
warnings.filterwarnings("ignore", message="SparseDataFrame")
1950+
return SparseDataFrame(self._series, index=self.index,
1951+
columns=self.columns, default_kind=kind,
1952+
default_fill_value=fill_value)
19461953

19471954
@deprecate_kwarg(old_arg_name='encoding', new_arg_name=None)
19481955
def to_stata(self, fname, convert_dates=None, write_index=True,

pandas/core/generic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,11 +1940,16 @@ def to_dense(self):
19401940
"""
19411941
Return dense representation of NDFrame (as opposed to sparse).
19421942
1943+
.. deprecated:: 0.25.0
1944+
19431945
Returns
19441946
-------
19451947
%(klass)s
19461948
Dense %(klass)s.
19471949
"""
1950+
warnings.warn("DataFrame/Series.to_dense is deprecated "
1951+
"and will be removed in a future version",
1952+
FutureWarning, stacklevel=2)
19481953
# compat
19491954
return self
19501955

pandas/core/groupby/ops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,9 @@ def _aggregate_series_fast(self, obj, func):
630630
group_index, _, ngroups = self.group_info
631631

632632
# avoids object / Series creation overhead
633-
dummy = obj._get_values(slice(None, 0)).to_dense()
633+
dummy = obj._get_values(slice(None, 0))
634634
indexer = get_group_index_sorter(group_index, ngroups)
635-
obj = obj._take(indexer).to_dense()
635+
obj = obj._take(indexer)
636636
group_index = algorithms.take_nd(
637637
group_index, indexer, allow_fill=False)
638638
grouper = reduction.SeriesGrouper(obj, func, group_index, ngroups,
@@ -879,7 +879,7 @@ def apply(self, f):
879879
class SeriesSplitter(DataSplitter):
880880

881881
def _chop(self, sdata, slice_obj):
882-
return sdata._get_values(slice_obj).to_dense()
882+
return sdata._get_values(slice_obj)
883883

884884

885885
class FrameSplitter(DataSplitter):

pandas/core/series.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,8 @@ def to_sparse(self, kind='block', fill_value=None):
15921592
"""
15931593
Convert Series to SparseSeries.
15941594
1595+
.. deprecated:: 0.25.0
1596+
15951597
Parameters
15961598
----------
15971599
kind : {'block', 'integer'}, default 'block'
@@ -1603,12 +1605,17 @@ def to_sparse(self, kind='block', fill_value=None):
16031605
SparseSeries
16041606
Sparse representation of the Series.
16051607
"""
1608+
1609+
warnings.warn("Series.to_sparse is deprecated and will be removed "
1610+
"in a future version", FutureWarning, stacklevel=2)
16061611
from pandas.core.sparse.series import SparseSeries
16071612

16081613
values = SparseArray(self, kind=kind, fill_value=fill_value)
1609-
return SparseSeries(
1610-
values, index=self.index, name=self.name
1611-
).__finalize__(self)
1614+
with warnings.catch_warnings():
1615+
warnings.filterwarnings("ignore", message="SparseSeries")
1616+
return SparseSeries(
1617+
values, index=self.index, name=self.name
1618+
).__finalize__(self)
16121619

16131620
def _set_name(self, name, inplace=False):
16141621
"""

pandas/tests/arrays/sparse/test_arithmetics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
@pytest.mark.filterwarnings("ignore:Sparse:FutureWarning")
12+
@pytest.mark.filterwarnings("ignore:Series.to_sparse:FutureWarning")
1213
class TestSparseArrayArithmetics:
1314

1415
_base = np.array

pandas/tests/generic/test_generic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,17 @@ def test_axis_classmethods(self, box):
918918
assert obj._get_axis_name(v) == box._get_axis_name(v)
919919
assert obj._get_block_manager_axis(v) == \
920920
box._get_block_manager_axis(v)
921+
922+
def test_deprecated_to_dense(self):
923+
# GH 26557: DEPR
924+
# Deprecated 0.25.0
925+
926+
df = pd.DataFrame({"A": [1, 2, 3]})
927+
with tm.assert_produces_warning(FutureWarning):
928+
result = df.to_dense()
929+
tm.assert_frame_equal(result, df)
930+
931+
ser = pd.Series([1, 2, 3])
932+
with tm.assert_produces_warning(FutureWarning):
933+
result = ser.to_dense()
934+
tm.assert_series_equal(result, ser)

pandas/tests/io/json/test_pandas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,8 @@ def test_datetime_tz(self):
10131013
assert stz.to_json() == s_naive.to_json()
10141014

10151015
@pytest.mark.filterwarnings("ignore:Sparse:FutureWarning")
1016+
@pytest.mark.filterwarnings("ignore:DataFrame.to_sparse:FutureWarning")
1017+
@pytest.mark.filterwarnings("ignore:Series.to_sparse:FutureWarning")
10161018
def test_sparse(self):
10171019
# GH4377 df.to_json segfaults with non-ndarray blocks
10181020
df = pd.DataFrame(np.random.randn(10, 4))

pandas/tests/io/test_packers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ def test_dataframe_duplicate_column_names(self):
551551

552552

553553
@pytest.mark.filterwarnings("ignore:Sparse:FutureWarning")
554+
@pytest.mark.filterwarnings("ignore:Series.to_sparse:FutureWarning")
555+
@pytest.mark.filterwarnings("ignore:DataFrame.to_sparse:FutureWarning")
554556
class TestSparse(TestPackers):
555557

556558
def _check_roundtrip(self, obj, comparator, **kwargs):

pandas/tests/io/test_pytables.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
"ignore:object name:tables.exceptions.NaturalNameWarning"
5252
)
5353
ignore_sparse = pytest.mark.filterwarnings("ignore:Sparse:FutureWarning")
54+
ignore_dataframe_tosparse = pytest.mark.filterwarnings(
55+
"ignore:DataFrame.to_sparse:FutureWarning"
56+
)
57+
ignore_series_tosparse = pytest.mark.filterwarnings(
58+
"ignore:Series.to_sparse:FutureWarning"
59+
)
5460

5561
# contextmanager to ensure the file cleanup
5662

@@ -2245,6 +2251,7 @@ def test_series(self):
22452251
check_index_type=False)
22462252

22472253
@ignore_sparse
2254+
@ignore_series_tosparse
22482255
def test_sparse_series(self):
22492256

22502257
s = tm.makeStringSeries()
@@ -2262,6 +2269,7 @@ def test_sparse_series(self):
22622269
check_series_type=True)
22632270

22642271
@ignore_sparse
2272+
@ignore_dataframe_tosparse
22652273
def test_sparse_frame(self):
22662274

22672275
s = tm.makeDataFrame()
@@ -2601,6 +2609,7 @@ def test_overwrite_node(self):
26012609
tm.assert_series_equal(store['a'], ts)
26022610

26032611
@ignore_sparse
2612+
@ignore_dataframe_tosparse
26042613
def test_sparse_with_compression(self):
26052614

26062615
# GH 2931
@@ -3746,6 +3755,7 @@ def test_start_stop_multiple(self):
37463755
tm.assert_frame_equal(result, expected)
37473756

37483757
@ignore_sparse
3758+
@ignore_dataframe_tosparse
37493759
def test_start_stop_fixed(self):
37503760

37513761
with ensure_clean_store(self.path) as store:

0 commit comments

Comments
 (0)