Skip to content

Commit 1d7e451

Browse files
gfyoungjreback
authored andcommitted
DEPR: Deprecate SparseList. (pandas-dev#14007)
Closes pandas-devgh-13784.
1 parent 471c4e7 commit 1d7e451

File tree

4 files changed

+80
-55
lines changed

4 files changed

+80
-55
lines changed

doc/source/sparse.rst

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ can be converted back to a regular ndarray by calling ``to_dense``:
9090
SparseList
9191
----------
9292

93+
.. note:: The ``SparseList`` class has been deprecated and will be removed in a future version.
94+
9395
``SparseList`` is a list-like data structure for managing a dynamic collection
9496
of SparseArrays. To create one, simply call the ``SparseList`` constructor with
9597
a ``fill_value`` (defaulting to ``NaN``):

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ Deprecations
780780
- ``Categorical.reshape`` has been deprecated and will be removed in a subsequent release (:issue:`12882`)
781781
- ``Series.reshape`` has been deprecated and will be removed in a subsequent release (:issue:`12882`)
782782

783+
- ``SparseList`` has been deprecated and will be removed in a future version (:issue:`13784`)
783784
- ``DataFrame.to_html()`` and ``DataFrame.to_latex()`` have dropped the ``colSpace`` parameter in favor of ``col_space`` (:issue:`13857`)
784785
- ``DataFrame.to_sql()`` has deprecated the ``flavor`` parameter, as it is superfluous when SQLAlchemy is not installed (:issue:`13611`)
785786
- ``compact_ints`` and ``use_unsigned`` have been deprecated in ``pd.read_csv()`` and will be removed in a future version (:issue:`13320`)

pandas/sparse/list.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
import numpy as np
23
from pandas.core.base import PandasObject
34
from pandas.formats.printing import pprint_thing
@@ -20,6 +21,11 @@ class SparseList(PandasObject):
2021
"""
2122

2223
def __init__(self, data=None, fill_value=np.nan):
24+
25+
# see gh-13784
26+
warnings.warn("SparseList is deprecated and will be removed "
27+
"in a future version", FutureWarning, stacklevel=2)
28+
2329
self.fill_value = fill_value
2430
self._chunks = []
2531

pandas/sparse/tests/test_list.py

+71-55
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,99 @@ def setUp(self):
1616
self.na_data = np.array([nan, nan, 1, 2, 3, nan, 4, 5, nan, 6])
1717
self.zero_data = np.array([0, 0, 1, 2, 3, 0, 4, 5, 0, 6])
1818

19+
def test_deprecation(self):
20+
# see gh-13784
21+
with tm.assert_produces_warning(FutureWarning):
22+
SparseList()
23+
1924
def test_constructor(self):
20-
lst1 = SparseList(self.na_data[:5])
21-
exp = SparseList()
25+
with tm.assert_produces_warning(FutureWarning):
26+
lst1 = SparseList(self.na_data[:5])
27+
with tm.assert_produces_warning(FutureWarning):
28+
exp = SparseList()
29+
2230
exp.append(self.na_data[:5])
2331
tm.assert_sp_list_equal(lst1, exp)
2432

2533
def test_len(self):
26-
arr = self.na_data
27-
splist = SparseList()
28-
splist.append(arr[:5])
29-
self.assertEqual(len(splist), 5)
30-
splist.append(arr[5])
31-
self.assertEqual(len(splist), 6)
32-
splist.append(arr[6:])
33-
self.assertEqual(len(splist), 10)
34+
with tm.assert_produces_warning(FutureWarning):
35+
arr = self.na_data
36+
splist = SparseList()
37+
splist.append(arr[:5])
38+
self.assertEqual(len(splist), 5)
39+
splist.append(arr[5])
40+
self.assertEqual(len(splist), 6)
41+
splist.append(arr[6:])
42+
self.assertEqual(len(splist), 10)
3443

3544
def test_append_na(self):
36-
arr = self.na_data
37-
splist = SparseList()
38-
splist.append(arr[:5])
39-
splist.append(arr[5])
40-
splist.append(arr[6:])
45+
with tm.assert_produces_warning(FutureWarning):
46+
arr = self.na_data
47+
splist = SparseList()
48+
splist.append(arr[:5])
49+
splist.append(arr[5])
50+
splist.append(arr[6:])
4151

42-
sparr = splist.to_array()
43-
tm.assert_sp_array_equal(sparr, SparseArray(arr))
52+
sparr = splist.to_array()
53+
tm.assert_sp_array_equal(sparr, SparseArray(arr))
4454

4555
def test_append_zero(self):
46-
arr = self.zero_data
47-
splist = SparseList(fill_value=0)
48-
splist.append(arr[:5])
49-
splist.append(arr[5])
50-
splist.append(arr[6:])
56+
with tm.assert_produces_warning(FutureWarning):
57+
arr = self.zero_data
58+
splist = SparseList(fill_value=0)
59+
splist.append(arr[:5])
60+
splist.append(arr[5])
61+
splist.append(arr[6:])
5162

52-
sparr = splist.to_array()
53-
tm.assert_sp_array_equal(sparr, SparseArray(arr, fill_value=0))
63+
sparr = splist.to_array()
64+
tm.assert_sp_array_equal(sparr, SparseArray(arr, fill_value=0))
5465

5566
def test_consolidate(self):
56-
arr = self.na_data
57-
exp_sparr = SparseArray(arr)
67+
with tm.assert_produces_warning(FutureWarning,
68+
check_stacklevel=False):
69+
arr = self.na_data
70+
exp_sparr = SparseArray(arr)
5871

59-
splist = SparseList()
60-
splist.append(arr[:5])
61-
splist.append(arr[5])
62-
splist.append(arr[6:])
72+
splist = SparseList()
73+
splist.append(arr[:5])
74+
splist.append(arr[5])
75+
splist.append(arr[6:])
6376

64-
consol = splist.consolidate(inplace=False)
65-
self.assertEqual(consol.nchunks, 1)
66-
self.assertEqual(splist.nchunks, 3)
67-
tm.assert_sp_array_equal(consol.to_array(), exp_sparr)
77+
consol = splist.consolidate(inplace=False)
78+
self.assertEqual(consol.nchunks, 1)
79+
self.assertEqual(splist.nchunks, 3)
80+
tm.assert_sp_array_equal(consol.to_array(), exp_sparr)
6881

69-
splist.consolidate()
70-
self.assertEqual(splist.nchunks, 1)
71-
tm.assert_sp_array_equal(splist.to_array(), exp_sparr)
82+
splist.consolidate()
83+
self.assertEqual(splist.nchunks, 1)
84+
tm.assert_sp_array_equal(splist.to_array(), exp_sparr)
7285

7386
def test_copy(self):
74-
arr = self.na_data
75-
exp_sparr = SparseArray(arr)
87+
with tm.assert_produces_warning(FutureWarning,
88+
check_stacklevel=False):
89+
arr = self.na_data
90+
exp_sparr = SparseArray(arr)
7691

77-
splist = SparseList()
78-
splist.append(arr[:5])
79-
splist.append(arr[5])
92+
splist = SparseList()
93+
splist.append(arr[:5])
94+
splist.append(arr[5])
8095

81-
cp = splist.copy()
82-
cp.append(arr[6:])
83-
self.assertEqual(splist.nchunks, 2)
84-
tm.assert_sp_array_equal(cp.to_array(), exp_sparr)
96+
cp = splist.copy()
97+
cp.append(arr[6:])
98+
self.assertEqual(splist.nchunks, 2)
99+
tm.assert_sp_array_equal(cp.to_array(), exp_sparr)
85100

86101
def test_getitem(self):
87-
arr = self.na_data
88-
splist = SparseList()
89-
splist.append(arr[:5])
90-
splist.append(arr[5])
91-
splist.append(arr[6:])
92-
93-
for i in range(len(arr)):
94-
tm.assert_almost_equal(splist[i], arr[i])
95-
tm.assert_almost_equal(splist[-i], arr[-i])
102+
with tm.assert_produces_warning(FutureWarning):
103+
arr = self.na_data
104+
splist = SparseList()
105+
splist.append(arr[:5])
106+
splist.append(arr[5])
107+
splist.append(arr[6:])
108+
109+
for i in range(len(arr)):
110+
tm.assert_almost_equal(splist[i], arr[i])
111+
tm.assert_almost_equal(splist[-i], arr[-i])
96112

97113

98114
if __name__ == '__main__':

0 commit comments

Comments
 (0)