@@ -36,6 +36,10 @@ def test_getitem(self):
36
36
exp = orig [orig % 2 == 1 ].to_sparse ()
37
37
tm .assert_sp_series_equal (result , exp )
38
38
39
+ # sparse array
40
+ result = sparse [pd .SparseArray (sparse % 2 == 1 , dtype = bool )]
41
+ tm .assert_sp_series_equal (result , exp )
42
+
39
43
def test_getitem_slice (self ):
40
44
orig = self .orig
41
45
sparse = self .sparse
@@ -68,6 +72,10 @@ def test_getitem_fill_value(self):
68
72
exp = orig [orig % 2 == 1 ].to_sparse (fill_value = 0 )
69
73
tm .assert_sp_series_equal (result , exp )
70
74
75
+ # sparse array
76
+ result = sparse [pd .SparseArray (sparse % 2 == 1 , dtype = bool )]
77
+ tm .assert_sp_series_equal (result , exp )
78
+
71
79
def test_getitem_ellipsis (self ):
72
80
# GH 9467
73
81
s = pd .SparseSeries ([1 , np .nan , 2 , 0 , np .nan ])
@@ -116,6 +124,10 @@ def test_loc(self):
116
124
exp = orig .loc [orig % 2 == 1 ].to_sparse ()
117
125
tm .assert_sp_series_equal (result , exp )
118
126
127
+ # sparse array
128
+ result = sparse .loc [pd .SparseArray (sparse % 2 == 1 , dtype = bool )]
129
+ tm .assert_sp_series_equal (result , exp )
130
+
119
131
def test_loc_index (self ):
120
132
orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ], index = list ('ABCDE' ))
121
133
sparse = orig .to_sparse ()
@@ -137,6 +149,10 @@ def test_loc_index(self):
137
149
exp = orig .loc [orig % 2 == 1 ].to_sparse ()
138
150
tm .assert_sp_series_equal (result , exp )
139
151
152
+ # sparse array
153
+ result = sparse [pd .SparseArray (sparse % 2 == 1 , dtype = bool )]
154
+ tm .assert_sp_series_equal (result , exp )
155
+
140
156
def test_loc_index_fill_value (self ):
141
157
orig = pd .Series ([1 , np .nan , 0 , 3 , 0 ], index = list ('ABCDE' ))
142
158
sparse = orig .to_sparse (fill_value = 0 )
@@ -368,6 +384,35 @@ def test_reindex_fill_value(self):
368
384
exp = orig .reindex (['A' , 'E' , 'C' , 'D' ]).to_sparse (fill_value = 0 )
369
385
tm .assert_sp_series_equal (res , exp )
370
386
387
+ def tests_indexing_with_sparse (self ):
388
+ # GH 13985
389
+
390
+ for kind in ['integer' , 'block' ]:
391
+ for fill in [True , False , np .nan ]:
392
+ arr = pd .SparseArray ([1 , 2 , 3 ], kind = kind )
393
+ indexer = pd .SparseArray ([True , False , True ], fill_value = fill ,
394
+ dtype = bool )
395
+
396
+ tm .assert_sp_array_equal (pd .SparseArray ([1 , 3 ], kind = kind ),
397
+ arr [indexer ])
398
+
399
+ s = pd .SparseSeries (arr , index = ['a' , 'b' , 'c' ],
400
+ dtype = np .float64 )
401
+ exp = pd .SparseSeries ([1 , 3 ], index = ['a' , 'c' ],
402
+ dtype = np .float64 , kind = kind )
403
+ tm .assert_sp_series_equal (s [indexer ], exp )
404
+ tm .assert_sp_series_equal (s .loc [indexer ], exp )
405
+ tm .assert_sp_series_equal (s .iloc [indexer ], exp )
406
+
407
+ indexer = pd .SparseSeries (indexer , index = ['a' , 'b' , 'c' ])
408
+ tm .assert_sp_series_equal (s [indexer ], exp )
409
+ tm .assert_sp_series_equal (s .loc [indexer ], exp )
410
+
411
+ msg = ("iLocation based boolean indexing cannot use an "
412
+ "indexable as a mask" )
413
+ with tm .assertRaisesRegexp (ValueError , msg ):
414
+ s .iloc [indexer ]
415
+
371
416
372
417
class TestSparseSeriesMultiIndexing (TestSparseSeriesIndexing ):
373
418
@@ -405,6 +450,10 @@ def test_getitem_multi(self):
405
450
exp = orig [orig % 2 == 1 ].to_sparse ()
406
451
tm .assert_sp_series_equal (result , exp )
407
452
453
+ # sparse array
454
+ result = sparse [pd .SparseArray (sparse % 2 == 1 , dtype = bool )]
455
+ tm .assert_sp_series_equal (result , exp )
456
+
408
457
def test_getitem_multi_tuple (self ):
409
458
orig = self .orig
410
459
sparse = self .sparse
@@ -454,6 +503,10 @@ def test_loc(self):
454
503
exp = orig .loc [orig % 2 == 1 ].to_sparse ()
455
504
tm .assert_sp_series_equal (result , exp )
456
505
506
+ # sparse array
507
+ result = sparse .loc [pd .SparseArray (sparse % 2 == 1 , dtype = bool )]
508
+ tm .assert_sp_series_equal (result , exp )
509
+
457
510
def test_loc_multi_tuple (self ):
458
511
orig = self .orig
459
512
sparse = self .sparse
@@ -578,6 +631,10 @@ def test_loc(self):
578
631
exp = orig .loc [orig .x % 2 == 1 ].to_sparse ()
579
632
tm .assert_sp_frame_equal (result , exp )
580
633
634
+ # sparse array
635
+ result = sparse .loc [pd .SparseArray (sparse .x % 2 == 1 , dtype = bool )]
636
+ tm .assert_sp_frame_equal (result , exp )
637
+
581
638
def test_loc_index (self ):
582
639
orig = pd .DataFrame ([[1 , np .nan , np .nan ],
583
640
[2 , 3 , np .nan ],
@@ -627,6 +684,10 @@ def test_loc_index(self):
627
684
exp = orig .loc [orig .x % 2 == 1 ].to_sparse ()
628
685
tm .assert_sp_frame_equal (result , exp )
629
686
687
+ # sparse array
688
+ result = sparse .loc [pd .SparseArray (sparse .x % 2 == 1 , dtype = bool )]
689
+ tm .assert_sp_frame_equal (result , exp )
690
+
630
691
def test_loc_slice (self ):
631
692
orig = pd .DataFrame ([[1 , np .nan , np .nan ],
632
693
[2 , 3 , np .nan ],
0 commit comments