@@ -10,9 +10,13 @@ class TestSparseSeriesIndexing(tm.TestCase):
10
10
11
11
_multiprocess_can_split_ = True
12
12
13
+ def setUp (self ):
14
+ self .orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ])
15
+ self .sparse = self .orig .to_sparse ()
16
+
13
17
def test_getitem (self ):
14
- orig = pd . Series ([ 1 , np . nan , np . nan , 3 , np . nan ])
15
- sparse = orig . to_sparse ()
18
+ orig = self . orig
19
+ sparse = self . sparse
16
20
17
21
self .assertEqual (sparse [0 ], 1 )
18
22
self .assertTrue (np .isnan (sparse [1 ]))
@@ -33,8 +37,9 @@ def test_getitem(self):
33
37
tm .assert_sp_series_equal (result , exp )
34
38
35
39
def test_getitem_slice (self ):
36
- orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ])
37
- sparse = orig .to_sparse ()
40
+ orig = self .orig
41
+ sparse = self .sparse
42
+
38
43
tm .assert_sp_series_equal (sparse [:2 ], orig [:2 ].to_sparse ())
39
44
tm .assert_sp_series_equal (sparse [4 :2 ], orig [4 :2 ].to_sparse ())
40
45
tm .assert_sp_series_equal (sparse [::2 ], orig [::2 ].to_sparse ())
@@ -84,8 +89,8 @@ def test_getitem_slice_fill_value(self):
84
89
orig [- 5 :].to_sparse (fill_value = 0 ))
85
90
86
91
def test_loc (self ):
87
- orig = pd . Series ([ 1 , np . nan , np . nan , 3 , np . nan ])
88
- sparse = orig . to_sparse ()
92
+ orig = self . orig
93
+ sparse = self . sparse
89
94
90
95
self .assertEqual (sparse .loc [0 ], 1 )
91
96
self .assertTrue (np .isnan (sparse .loc [1 ]))
@@ -154,19 +159,26 @@ def test_loc_index_fill_value(self):
154
159
tm .assert_sp_series_equal (result , exp )
155
160
156
161
def test_loc_slice (self ):
157
- orig = pd . Series ([ 1 , np . nan , np . nan , 3 , np . nan ])
158
- sparse = orig . to_sparse ()
162
+ orig = self . orig
163
+ sparse = self . sparse
159
164
tm .assert_sp_series_equal (sparse .loc [2 :], orig .loc [2 :].to_sparse ())
160
165
166
+ def test_loc_slice_index_fill_value (self ):
167
+ orig = pd .Series ([1 , np .nan , 0 , 3 , 0 ], index = list ('ABCDE' ))
168
+ sparse = orig .to_sparse (fill_value = 0 )
169
+
170
+ tm .assert_sp_series_equal (sparse .loc ['C' :],
171
+ orig .loc ['C' :].to_sparse (fill_value = 0 ))
172
+
161
173
def test_loc_slice_fill_value (self ):
162
174
orig = pd .Series ([1 , np .nan , 0 , 3 , 0 ])
163
175
sparse = orig .to_sparse (fill_value = 0 )
164
176
tm .assert_sp_series_equal (sparse .loc [2 :],
165
177
orig .loc [2 :].to_sparse (fill_value = 0 ))
166
178
167
179
def test_iloc (self ):
168
- orig = pd . Series ([ 1 , np . nan , np . nan , 3 , np . nan ])
169
- sparse = orig . to_sparse ()
180
+ orig = self . orig
181
+ sparse = self . sparse
170
182
171
183
self .assertEqual (sparse .iloc [3 ], 3 )
172
184
self .assertTrue (np .isnan (sparse .iloc [2 ]))
@@ -234,8 +246,9 @@ def test_at_fill_value(self):
234
246
self .assertEqual (sparse .at ['e' ], orig .at ['e' ])
235
247
236
248
def test_iat (self ):
237
- orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ])
238
- sparse = orig .to_sparse ()
249
+ orig = self .orig
250
+ sparse = self .sparse
251
+
239
252
self .assertEqual (sparse .iat [0 ], orig .iat [0 ])
240
253
self .assertTrue (np .isnan (sparse .iat [1 ]))
241
254
self .assertTrue (np .isnan (sparse .iat [2 ]))
@@ -356,6 +369,111 @@ def test_reindex_fill_value(self):
356
369
tm .assert_sp_series_equal (res , exp )
357
370
358
371
372
+ class TestSparseSeriesMultiIndexing (TestSparseSeriesIndexing ):
373
+
374
+ _multiprocess_can_split_ = True
375
+
376
+ def setUp (self ):
377
+ # Mi with duplicated values
378
+ idx = pd .MultiIndex .from_tuples ([('A' , 0 ), ('A' , 1 ), ('B' , 0 ),
379
+ ('C' , 0 ), ('C' , 1 )])
380
+ self .orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ], index = idx )
381
+ self .sparse = self .orig .to_sparse ()
382
+
383
+ def test_getitem_multi (self ):
384
+ orig = self .orig
385
+ sparse = self .sparse
386
+
387
+ self .assertEqual (sparse [0 ], orig [0 ])
388
+ self .assertTrue (np .isnan (sparse [1 ]))
389
+ self .assertEqual (sparse [3 ], orig [3 ])
390
+
391
+ tm .assert_sp_series_equal (sparse ['A' ], orig ['A' ].to_sparse ())
392
+ tm .assert_sp_series_equal (sparse ['B' ], orig ['B' ].to_sparse ())
393
+
394
+ result = sparse [[1 , 3 , 4 ]]
395
+ exp = orig [[1 , 3 , 4 ]].to_sparse ()
396
+ tm .assert_sp_series_equal (result , exp )
397
+
398
+ # dense array
399
+ result = sparse [orig % 2 == 1 ]
400
+ exp = orig [orig % 2 == 1 ].to_sparse ()
401
+ tm .assert_sp_series_equal (result , exp )
402
+
403
+ # sparse array (actuary it coerces to normal Series)
404
+ result = sparse [sparse % 2 == 1 ]
405
+ exp = orig [orig % 2 == 1 ].to_sparse ()
406
+ tm .assert_sp_series_equal (result , exp )
407
+
408
+ def test_getitem_multi_tuple (self ):
409
+ orig = self .orig
410
+ sparse = self .sparse
411
+
412
+ self .assertEqual (sparse ['C' , 0 ], orig ['C' , 0 ])
413
+ self .assertTrue (np .isnan (sparse ['A' , 1 ]))
414
+ self .assertTrue (np .isnan (sparse ['B' , 0 ]))
415
+
416
+ def test_getitems_slice_multi (self ):
417
+ orig = self .orig
418
+ sparse = self .sparse
419
+
420
+ tm .assert_sp_series_equal (sparse [2 :], orig [2 :].to_sparse ())
421
+ tm .assert_sp_series_equal (sparse .loc ['B' :], orig .loc ['B' :].to_sparse ())
422
+ tm .assert_sp_series_equal (sparse .loc ['C' :], orig .loc ['C' :].to_sparse ())
423
+
424
+ tm .assert_sp_series_equal (sparse .loc ['A' :'B' ],
425
+ orig .loc ['A' :'B' ].to_sparse ())
426
+ tm .assert_sp_series_equal (sparse .loc [:'B' ], orig .loc [:'B' ].to_sparse ())
427
+
428
+ def test_loc (self ):
429
+ # need to be override to use different label
430
+ orig = self .orig
431
+ sparse = self .sparse
432
+
433
+ tm .assert_sp_series_equal (sparse .loc ['A' ],
434
+ orig .loc ['A' ].to_sparse ())
435
+ tm .assert_sp_series_equal (sparse .loc ['B' ],
436
+ orig .loc ['B' ].to_sparse ())
437
+
438
+ result = sparse .loc [[1 , 3 , 4 ]]
439
+ exp = orig .loc [[1 , 3 , 4 ]].to_sparse ()
440
+ tm .assert_sp_series_equal (result , exp )
441
+
442
+ # exceeds the bounds
443
+ result = sparse .loc [[1 , 3 , 4 , 5 ]]
444
+ exp = orig .loc [[1 , 3 , 4 , 5 ]].to_sparse ()
445
+ tm .assert_sp_series_equal (result , exp )
446
+
447
+ # dense array
448
+ result = sparse .loc [orig % 2 == 1 ]
449
+ exp = orig .loc [orig % 2 == 1 ].to_sparse ()
450
+ tm .assert_sp_series_equal (result , exp )
451
+
452
+ # sparse array (actuary it coerces to normal Series)
453
+ result = sparse .loc [sparse % 2 == 1 ]
454
+ exp = orig .loc [orig % 2 == 1 ].to_sparse ()
455
+ tm .assert_sp_series_equal (result , exp )
456
+
457
+ def test_loc_multi_tuple (self ):
458
+ orig = self .orig
459
+ sparse = self .sparse
460
+
461
+ self .assertEqual (sparse .loc ['C' , 0 ], orig .loc ['C' , 0 ])
462
+ self .assertTrue (np .isnan (sparse .loc ['A' , 1 ]))
463
+ self .assertTrue (np .isnan (sparse .loc ['B' , 0 ]))
464
+
465
+ def test_loc_slice (self ):
466
+ orig = self .orig
467
+ sparse = self .sparse
468
+ tm .assert_sp_series_equal (sparse .loc ['A' :], orig .loc ['A' :].to_sparse ())
469
+ tm .assert_sp_series_equal (sparse .loc ['B' :], orig .loc ['B' :].to_sparse ())
470
+ tm .assert_sp_series_equal (sparse .loc ['C' :], orig .loc ['C' :].to_sparse ())
471
+
472
+ tm .assert_sp_series_equal (sparse .loc ['A' :'B' ],
473
+ orig .loc ['A' :'B' ].to_sparse ())
474
+ tm .assert_sp_series_equal (sparse .loc [:'B' ], orig .loc [:'B' ].to_sparse ())
475
+
476
+
359
477
class TestSparseDataFrameIndexing (tm .TestCase ):
360
478
361
479
_multiprocess_can_split_ = True
0 commit comments