forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_array.py
204 lines (158 loc) · 6.79 KB
/
test_array.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from pandas.compat import range
import re
from numpy import nan
import numpy as np
import operator
import warnings
from pandas.sparse.api import SparseArray
from pandas.util.testing import assert_almost_equal, assertRaisesRegexp
import pandas.util.testing as tm
class TestSparseArray(tm.TestCase):
_multiprocess_can_split_ = True
def setUp(self):
self.arr_data = np.array([nan, nan, 1, 2, 3, nan, 4, 5, nan, 6])
self.arr = SparseArray(self.arr_data)
self.zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
def test_get_item(self):
self.assertTrue(np.isnan(self.arr[1]))
self.assertEqual(self.arr[2], 1)
self.assertEqual(self.arr[7], 5)
self.assertEqual(self.zarr[0], 0)
self.assertEqual(self.zarr[2], 1)
self.assertEqual(self.zarr[7], 5)
errmsg = re.compile("bounds")
assertRaisesRegexp(IndexError, errmsg, lambda: self.arr[11])
assertRaisesRegexp(IndexError, errmsg, lambda: self.arr[-11])
self.assertEqual(self.arr[-1], self.arr[len(self.arr) - 1])
def test_take(self):
self.assertTrue(np.isnan(self.arr.take(0)))
self.assertTrue(np.isscalar(self.arr.take(2)))
self.assertEqual(self.arr.take(2), np.take(self.arr_data, 2))
self.assertEqual(self.arr.take(6), np.take(self.arr_data, 6))
tm.assert_sp_array_equal(self.arr.take([2, 3]),
SparseArray(np.take(self.arr_data, [2, 3])))
tm.assert_sp_array_equal(self.arr.take([0, 1, 2]),
SparseArray(np.take(self.arr_data,
[0, 1, 2])))
def test_bad_take(self):
assertRaisesRegexp(IndexError, "bounds", lambda: self.arr.take(11))
self.assertRaises(IndexError, lambda: self.arr.take(-11))
def test_set_item(self):
def setitem():
self.arr[5] = 3
def setslice():
self.arr[1:5] = 2
assertRaisesRegexp(TypeError, "item assignment", setitem)
assertRaisesRegexp(TypeError, "item assignment", setslice)
def test_constructor_from_sparse(self):
res = SparseArray(self.zarr)
self.assertEqual(res.fill_value, 0)
assert_almost_equal(res.sp_values, self.zarr.sp_values)
def test_constructor_copy(self):
cp = SparseArray(self.arr, copy=True)
cp.sp_values[:3] = 0
self.assertFalse((self.arr.sp_values[:3] == 0).any())
not_copy = SparseArray(self.arr)
not_copy.sp_values[:3] = 0
self.assertTrue((self.arr.sp_values[:3] == 0).all())
def test_astype(self):
res = self.arr.astype('f8')
res.sp_values[:3] = 27
self.assertFalse((self.arr.sp_values[:3] == 27).any())
assertRaisesRegexp(TypeError, "floating point", self.arr.astype, 'i8')
def test_copy_shallow(self):
arr2 = self.arr.copy(deep=False)
def _get_base(values):
base = values.base
while base.base is not None:
base = base.base
return base
assert (_get_base(arr2) is _get_base(self.arr))
def test_values_asarray(self):
assert_almost_equal(self.arr.values, self.arr_data)
assert_almost_equal(self.arr.to_dense(), self.arr_data)
assert_almost_equal(self.arr.sp_values, np.asarray(self.arr))
def test_getitem(self):
def _checkit(i):
assert_almost_equal(self.arr[i], self.arr.values[i])
for i in range(len(self.arr)):
_checkit(i)
_checkit(-i)
def test_getslice(self):
result = self.arr[:-3]
exp = SparseArray(self.arr.values[:-3])
tm.assert_sp_array_equal(result, exp)
result = self.arr[-4:]
exp = SparseArray(self.arr.values[-4:])
tm.assert_sp_array_equal(result, exp)
# two corner cases from Series
result = self.arr[-12:]
exp = SparseArray(self.arr)
tm.assert_sp_array_equal(result, exp)
result = self.arr[:-12]
exp = SparseArray(self.arr.values[:0])
tm.assert_sp_array_equal(result, exp)
def test_binary_operators(self):
data1 = np.random.randn(20)
data2 = np.random.randn(20)
data1[::2] = np.nan
data2[::3] = np.nan
arr1 = SparseArray(data1)
arr2 = SparseArray(data2)
data1[::2] = 3
data2[::3] = 3
farr1 = SparseArray(data1, fill_value=3)
farr2 = SparseArray(data2, fill_value=3)
def _check_op(op, first, second):
res = op(first, second)
exp = SparseArray(op(first.values, second.values),
fill_value=first.fill_value)
tm.assertIsInstance(res, SparseArray)
assert_almost_equal(res.values, exp.values)
res2 = op(first, second.values)
tm.assertIsInstance(res2, SparseArray)
tm.assert_sp_array_equal(res, res2)
res3 = op(first.values, second)
tm.assertIsInstance(res3, SparseArray)
tm.assert_sp_array_equal(res, res3)
res4 = op(first, 4)
tm.assertIsInstance(res4, SparseArray)
# ignore this if the actual op raises (e.g. pow)
try:
exp = op(first.values, 4)
exp_fv = op(first.fill_value, 4)
assert_almost_equal(res4.fill_value, exp_fv)
assert_almost_equal(res4.values, exp)
except ValueError:
pass
def _check_inplace_op(op):
tmp = arr1.copy()
self.assertRaises(NotImplementedError, op, tmp, arr2)
bin_ops = [operator.add, operator.sub, operator.mul, operator.truediv,
operator.floordiv, operator.pow]
for op in bin_ops:
_check_op(op, arr1, arr2)
_check_op(op, farr1, farr2)
inplace_ops = ['iadd', 'isub', 'imul', 'itruediv', 'ifloordiv', 'ipow']
for op in inplace_ops:
_check_inplace_op(getattr(operator, op))
def test_pickle(self):
def _check_roundtrip(obj):
unpickled = self.round_trip_pickle(obj)
tm.assert_sp_array_equal(unpickled, obj)
_check_roundtrip(self.arr)
_check_roundtrip(self.zarr)
def test_generator_warnings(self):
sp_arr = SparseArray([1, 2, 3])
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings(action='always',
category=DeprecationWarning)
warnings.filterwarnings(action='always',
category=PendingDeprecationWarning)
for _ in sp_arr:
pass
assert len(w) == 0
if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)