Skip to content

DEPR: Deprecate SparseList #14007

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
Aug 16, 2016
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
2 changes: 2 additions & 0 deletions doc/source/sparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ can be converted back to a regular ndarray by calling ``to_dense``:
SparseList
----------

.. note:: The ``SparseList`` class has been deprecated and will be removed in a future version.

``SparseList`` is a list-like data structure for managing a dynamic collection
of SparseArrays. To create one, simply call the ``SparseList`` constructor with
a ``fill_value`` (defaulting to ``NaN``):
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ Deprecations
- ``Categorical.reshape`` has been deprecated and will be removed in a subsequent release (:issue:`12882`)
- ``Series.reshape`` has been deprecated and will be removed in a subsequent release (:issue:`12882`)

- ``SparseList`` has been deprecated and will be removed in a future version (:issue:`13784`)
- ``DataFrame.to_html()`` and ``DataFrame.to_latex()`` have dropped the ``colSpace`` parameter in favor of ``col_space`` (:issue:`13857`)
- ``DataFrame.to_sql()`` has deprecated the ``flavor`` parameter, as it is superfluous when SQLAlchemy is not installed (:issue:`13611`)
- ``compact_ints`` and ``use_unsigned`` have been deprecated in ``pd.read_csv()`` and will be removed in a future version (:issue:`13320`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/sparse/list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
import numpy as np
from pandas.core.base import PandasObject
from pandas.formats.printing import pprint_thing
Expand All @@ -20,6 +21,11 @@ class SparseList(PandasObject):
"""

def __init__(self, data=None, fill_value=np.nan):

# see gh-13784
warnings.warn("SparseList is deprecated and will be removed "
"in a future version", FutureWarning, stacklevel=2)

self.fill_value = fill_value
self._chunks = []

Expand Down
126 changes: 71 additions & 55 deletions pandas/sparse/tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,99 @@ def setUp(self):
self.na_data = np.array([nan, nan, 1, 2, 3, nan, 4, 5, nan, 6])
self.zero_data = np.array([0, 0, 1, 2, 3, 0, 4, 5, 0, 6])

def test_deprecation(self):
# see gh-13784
with tm.assert_produces_warning(FutureWarning):
SparseList()

def test_constructor(self):
lst1 = SparseList(self.na_data[:5])
exp = SparseList()
with tm.assert_produces_warning(FutureWarning):
lst1 = SparseList(self.na_data[:5])
with tm.assert_produces_warning(FutureWarning):
exp = SparseList()

exp.append(self.na_data[:5])
tm.assert_sp_list_equal(lst1, exp)

def test_len(self):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
self.assertEqual(len(splist), 5)
splist.append(arr[5])
self.assertEqual(len(splist), 6)
splist.append(arr[6:])
self.assertEqual(len(splist), 10)
with tm.assert_produces_warning(FutureWarning):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
self.assertEqual(len(splist), 5)
splist.append(arr[5])
self.assertEqual(len(splist), 6)
splist.append(arr[6:])
self.assertEqual(len(splist), 10)

def test_append_na(self):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])
with tm.assert_produces_warning(FutureWarning):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])

sparr = splist.to_array()
tm.assert_sp_array_equal(sparr, SparseArray(arr))
sparr = splist.to_array()
tm.assert_sp_array_equal(sparr, SparseArray(arr))

def test_append_zero(self):
arr = self.zero_data
splist = SparseList(fill_value=0)
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])
with tm.assert_produces_warning(FutureWarning):
arr = self.zero_data
splist = SparseList(fill_value=0)
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])

sparr = splist.to_array()
tm.assert_sp_array_equal(sparr, SparseArray(arr, fill_value=0))
sparr = splist.to_array()
tm.assert_sp_array_equal(sparr, SparseArray(arr, fill_value=0))

def test_consolidate(self):
arr = self.na_data
exp_sparr = SparseArray(arr)
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
arr = self.na_data
exp_sparr = SparseArray(arr)

splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])
splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])

consol = splist.consolidate(inplace=False)
self.assertEqual(consol.nchunks, 1)
self.assertEqual(splist.nchunks, 3)
tm.assert_sp_array_equal(consol.to_array(), exp_sparr)
consol = splist.consolidate(inplace=False)
self.assertEqual(consol.nchunks, 1)
self.assertEqual(splist.nchunks, 3)
tm.assert_sp_array_equal(consol.to_array(), exp_sparr)

splist.consolidate()
self.assertEqual(splist.nchunks, 1)
tm.assert_sp_array_equal(splist.to_array(), exp_sparr)
splist.consolidate()
self.assertEqual(splist.nchunks, 1)
tm.assert_sp_array_equal(splist.to_array(), exp_sparr)

def test_copy(self):
arr = self.na_data
exp_sparr = SparseArray(arr)
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
arr = self.na_data
exp_sparr = SparseArray(arr)

splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])

cp = splist.copy()
cp.append(arr[6:])
self.assertEqual(splist.nchunks, 2)
tm.assert_sp_array_equal(cp.to_array(), exp_sparr)
cp = splist.copy()
cp.append(arr[6:])
self.assertEqual(splist.nchunks, 2)
tm.assert_sp_array_equal(cp.to_array(), exp_sparr)

def test_getitem(self):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])

for i in range(len(arr)):
tm.assert_almost_equal(splist[i], arr[i])
tm.assert_almost_equal(splist[-i], arr[-i])
with tm.assert_produces_warning(FutureWarning):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
splist.append(arr[5])
splist.append(arr[6:])

for i in range(len(arr)):
tm.assert_almost_equal(splist[i], arr[i])
tm.assert_almost_equal(splist[-i], arr[-i])


if __name__ == '__main__':
Expand Down