forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_list.py
111 lines (89 loc) · 3.58 KB
/
test_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from pandas.compat import range
from numpy import nan
import numpy as np
from pandas.core.sparse.api import SparseList, SparseArray
import pandas.util.testing as tm
class TestSparseList(object):
def setup_method(self, method):
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):
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):
with tm.assert_produces_warning(FutureWarning):
arr = self.na_data
splist = SparseList()
splist.append(arr[:5])
assert len(splist) == 5
splist.append(arr[5])
assert len(splist) == 6
splist.append(arr[6:])
assert len(splist) == 10
def test_append_na(self):
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))
def test_append_zero(self):
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:])
# list always produces int64, but SA constructor
# is platform dtype aware
sparr = splist.to_array()
exp = SparseArray(arr, fill_value=0)
tm.assert_sp_array_equal(sparr, exp, check_dtype=False)
def test_consolidate(self):
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:])
consol = splist.consolidate(inplace=False)
assert consol.nchunks == 1
assert splist.nchunks == 3
tm.assert_sp_array_equal(consol.to_array(), exp_sparr)
splist.consolidate()
assert splist.nchunks == 1
tm.assert_sp_array_equal(splist.to_array(), exp_sparr)
def test_copy(self):
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])
cp = splist.copy()
cp.append(arr[6:])
assert splist.nchunks == 2
tm.assert_sp_array_equal(cp.to_array(), exp_sparr)
def test_getitem(self):
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])