1111import pandas .util .testing as tm
1212
1313
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-
2314class TestSparseArray (tm .TestCase ):
2415 _multiprocess_can_split_ = True
2516
@@ -29,11 +20,32 @@ def setUp(self):
2920 self .zarr = SparseArray ([0 , 0 , 1 , 2 , 3 , 0 , 4 , 5 , 0 , 6 ], fill_value = 0 )
3021
3122 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+
3232 errmsg = re .compile ("bounds" )
3333 assertRaisesRegexp (IndexError , errmsg , lambda : self .arr [11 ])
3434 assertRaisesRegexp (IndexError , errmsg , lambda : self .arr [- 11 ])
3535 self .assertEqual (self .arr [- 1 ], self .arr [len (self .arr ) - 1 ])
3636
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+
3749 def test_bad_take (self ):
3850 assertRaisesRegexp (IndexError , "bounds" , lambda : self .arr .take (11 ))
3951 self .assertRaises (IndexError , lambda : self .arr .take (- 11 ))
@@ -96,20 +108,20 @@ def _checkit(i):
96108 def test_getslice (self ):
97109 result = self .arr [:- 3 ]
98110 exp = SparseArray (self .arr .values [:- 3 ])
99- assert_sp_array_equal (result , exp )
111+ tm . assert_sp_array_equal (result , exp )
100112
101113 result = self .arr [- 4 :]
102114 exp = SparseArray (self .arr .values [- 4 :])
103- assert_sp_array_equal (result , exp )
115+ tm . assert_sp_array_equal (result , exp )
104116
105117 # two corner cases from Series
106118 result = self .arr [- 12 :]
107119 exp = SparseArray (self .arr )
108- assert_sp_array_equal (result , exp )
120+ tm . assert_sp_array_equal (result , exp )
109121
110122 result = self .arr [:- 12 ]
111123 exp = SparseArray (self .arr .values [:0 ])
112- assert_sp_array_equal (result , exp )
124+ tm . assert_sp_array_equal (result , exp )
113125
114126 def test_binary_operators (self ):
115127 data1 = np .random .randn (20 )
@@ -134,11 +146,11 @@ def _check_op(op, first, second):
134146
135147 res2 = op (first , second .values )
136148 tm .assertIsInstance (res2 , SparseArray )
137- assert_sp_array_equal (res , res2 )
149+ tm . assert_sp_array_equal (res , res2 )
138150
139151 res3 = op (first .values , second )
140152 tm .assertIsInstance (res3 , SparseArray )
141- assert_sp_array_equal (res , res3 )
153+ tm . assert_sp_array_equal (res , res3 )
142154
143155 res4 = op (first , 4 )
144156 tm .assertIsInstance (res4 , SparseArray )
@@ -169,7 +181,7 @@ def _check_inplace_op(op):
169181 def test_pickle (self ):
170182 def _check_roundtrip (obj ):
171183 unpickled = self .round_trip_pickle (obj )
172- assert_sp_array_equal (unpickled , obj )
184+ tm . assert_sp_array_equal (unpickled , obj )
173185
174186 _check_roundtrip (self .arr )
175187 _check_roundtrip (self .zarr )
0 commit comments