11
11
import pandas .util .testing as tm
12
12
13
13
14
- def assert_sp_array_equal (left , right ):
15
- assert_almost_equal (left .sp_values , right .sp_values )
16
- assert (left .sp_index .equals (right .sp_index ))
17
- if np .isnan (left .fill_value ):
18
- assert (np .isnan (right .fill_value ))
19
- else :
20
- assert (left .fill_value == right .fill_value )
21
-
22
-
23
14
class TestSparseArray (tm .TestCase ):
24
15
_multiprocess_can_split_ = True
25
16
@@ -29,11 +20,32 @@ def setUp(self):
29
20
self .zarr = SparseArray ([0 , 0 , 1 , 2 , 3 , 0 , 4 , 5 , 0 , 6 ], fill_value = 0 )
30
21
31
22
def test_get_item (self ):
23
+
24
+ self .assertTrue (np .isnan (self .arr [1 ]))
25
+ self .assertEqual (self .arr [2 ], 1 )
26
+ self .assertEqual (self .arr [7 ], 5 )
27
+
28
+ self .assertEqual (self .zarr [0 ], 0 )
29
+ self .assertEqual (self .zarr [2 ], 1 )
30
+ self .assertEqual (self .zarr [7 ], 5 )
31
+
32
32
errmsg = re .compile ("bounds" )
33
33
assertRaisesRegexp (IndexError , errmsg , lambda : self .arr [11 ])
34
34
assertRaisesRegexp (IndexError , errmsg , lambda : self .arr [- 11 ])
35
35
self .assertEqual (self .arr [- 1 ], self .arr [len (self .arr ) - 1 ])
36
36
37
+ def test_take (self ):
38
+ self .assertTrue (np .isnan (self .arr .take (0 )))
39
+ self .assertTrue (np .isscalar (self .arr .take (2 )))
40
+ self .assertEqual (self .arr .take (2 ), np .take (self .arr_data , 2 ))
41
+ self .assertEqual (self .arr .take (6 ), np .take (self .arr_data , 6 ))
42
+
43
+ tm .assert_sp_array_equal (self .arr .take ([2 , 3 ]),
44
+ SparseArray (np .take (self .arr_data , [2 , 3 ])))
45
+ tm .assert_sp_array_equal (self .arr .take ([0 , 1 , 2 ]),
46
+ SparseArray (np .take (self .arr_data ,
47
+ [0 , 1 , 2 ])))
48
+
37
49
def test_bad_take (self ):
38
50
assertRaisesRegexp (IndexError , "bounds" , lambda : self .arr .take (11 ))
39
51
self .assertRaises (IndexError , lambda : self .arr .take (- 11 ))
@@ -96,20 +108,20 @@ def _checkit(i):
96
108
def test_getslice (self ):
97
109
result = self .arr [:- 3 ]
98
110
exp = SparseArray (self .arr .values [:- 3 ])
99
- assert_sp_array_equal (result , exp )
111
+ tm . assert_sp_array_equal (result , exp )
100
112
101
113
result = self .arr [- 4 :]
102
114
exp = SparseArray (self .arr .values [- 4 :])
103
- assert_sp_array_equal (result , exp )
115
+ tm . assert_sp_array_equal (result , exp )
104
116
105
117
# two corner cases from Series
106
118
result = self .arr [- 12 :]
107
119
exp = SparseArray (self .arr )
108
- assert_sp_array_equal (result , exp )
120
+ tm . assert_sp_array_equal (result , exp )
109
121
110
122
result = self .arr [:- 12 ]
111
123
exp = SparseArray (self .arr .values [:0 ])
112
- assert_sp_array_equal (result , exp )
124
+ tm . assert_sp_array_equal (result , exp )
113
125
114
126
def test_binary_operators (self ):
115
127
data1 = np .random .randn (20 )
@@ -134,11 +146,11 @@ def _check_op(op, first, second):
134
146
135
147
res2 = op (first , second .values )
136
148
tm .assertIsInstance (res2 , SparseArray )
137
- assert_sp_array_equal (res , res2 )
149
+ tm . assert_sp_array_equal (res , res2 )
138
150
139
151
res3 = op (first .values , second )
140
152
tm .assertIsInstance (res3 , SparseArray )
141
- assert_sp_array_equal (res , res3 )
153
+ tm . assert_sp_array_equal (res , res3 )
142
154
143
155
res4 = op (first , 4 )
144
156
tm .assertIsInstance (res4 , SparseArray )
@@ -169,7 +181,7 @@ def _check_inplace_op(op):
169
181
def test_pickle (self ):
170
182
def _check_roundtrip (obj ):
171
183
unpickled = self .round_trip_pickle (obj )
172
- assert_sp_array_equal (unpickled , obj )
184
+ tm . assert_sp_array_equal (unpickled , obj )
173
185
174
186
_check_roundtrip (self .arr )
175
187
_check_roundtrip (self .zarr )
0 commit comments