@@ -262,6 +262,19 @@ def test_constructor_bool(self):
262
262
self .assertEqual (dense .dtype , bool )
263
263
tm .assert_numpy_array_equal (dense , data )
264
264
265
+ def test_constructor_bool_fill_value (self ):
266
+ arr = SparseArray ([True , False , True ], dtype = None )
267
+ self .assertEqual (arr .dtype , np .bool )
268
+ self .assertFalse (arr .fill_value )
269
+
270
+ arr = SparseArray ([True , False , True ], dtype = np .bool )
271
+ self .assertEqual (arr .dtype , np .bool )
272
+ self .assertFalse (arr .fill_value )
273
+
274
+ arr = SparseArray ([True , False , True ], dtype = np .bool , fill_value = True )
275
+ self .assertEqual (arr .dtype , np .bool )
276
+ self .assertTrue (arr .fill_value )
277
+
265
278
def test_constructor_float32 (self ):
266
279
# GH 10648
267
280
data = np .array ([1. , np .nan , 3 ], dtype = np .float32 )
@@ -522,6 +535,31 @@ def _check_numeric_ops(self, a, b, a_dense, b_dense):
522
535
tm .assert_numpy_array_equal ((a ** b ).to_dense (), a_dense ** b_dense )
523
536
tm .assert_numpy_array_equal ((b ** a ).to_dense (), b_dense ** a_dense )
524
537
538
+ def _check_comparison_ops (self , a , b , a_dense , b_dense ):
539
+
540
+ def _check (res ):
541
+ tm .assertIsInstance (res , SparseArray )
542
+ self .assertEqual (res .dtype , np .bool )
543
+ self .assertIsInstance (res .fill_value , bool )
544
+
545
+ _check (a == b )
546
+ tm .assert_numpy_array_equal ((a == b ).to_dense (), a_dense == b_dense )
547
+
548
+ _check (a != b )
549
+ tm .assert_numpy_array_equal ((a != b ).to_dense (), a_dense != b_dense )
550
+
551
+ _check (a >= b )
552
+ tm .assert_numpy_array_equal ((a >= b ).to_dense (), a_dense >= b_dense )
553
+
554
+ _check (a <= b )
555
+ tm .assert_numpy_array_equal ((a <= b ).to_dense (), a_dense <= b_dense )
556
+
557
+ _check (a > b )
558
+ tm .assert_numpy_array_equal ((a > b ).to_dense (), a_dense > b_dense )
559
+
560
+ _check (a < b )
561
+ tm .assert_numpy_array_equal ((a < b ).to_dense (), a_dense < b_dense )
562
+
525
563
def test_float_scalar (self ):
526
564
values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
527
565
@@ -541,6 +579,25 @@ def test_float_scalar(self):
541
579
self ._check_numeric_ops (a , 0 , values , 0 )
542
580
self ._check_numeric_ops (a , 3 , values , 3 )
543
581
582
+ def test_float_scalar_comparison (self ):
583
+ values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
584
+
585
+ for kind in ['integer' , 'block' ]:
586
+ a = SparseArray (values , kind = kind )
587
+ self ._check_comparison_ops (a , 1 , values , 1 )
588
+ self ._check_comparison_ops (a , 0 , values , 0 )
589
+ self ._check_comparison_ops (a , 3 , values , 3 )
590
+
591
+ a = SparseArray (values , kind = kind , fill_value = 0 )
592
+ self ._check_comparison_ops (a , 1 , values , 1 )
593
+ self ._check_comparison_ops (a , 0 , values , 0 )
594
+ self ._check_comparison_ops (a , 3 , values , 3 )
595
+
596
+ a = SparseArray (values , kind = kind , fill_value = 2 )
597
+ self ._check_comparison_ops (a , 1 , values , 1 )
598
+ self ._check_comparison_ops (a , 0 , values , 0 )
599
+ self ._check_comparison_ops (a , 3 , values , 3 )
600
+
544
601
def test_float_same_index (self ):
545
602
# when sp_index are the same
546
603
for kind in ['integer' , 'block' ]:
@@ -558,6 +615,23 @@ def test_float_same_index(self):
558
615
b = SparseArray (rvalues , kind = kind , fill_value = 0 )
559
616
self ._check_numeric_ops (a , b , values , rvalues )
560
617
618
+ def test_float_same_index_comparison (self ):
619
+ # when sp_index are the same
620
+ for kind in ['integer' , 'block' ]:
621
+ values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
622
+ rvalues = np .array ([np .nan , 2 , 3 , 4 , np .nan , 0 , 1 , 3 , 2 , np .nan ])
623
+
624
+ a = SparseArray (values , kind = kind )
625
+ b = SparseArray (rvalues , kind = kind )
626
+ self ._check_comparison_ops (a , b , values , rvalues )
627
+
628
+ values = np .array ([0. , 1. , 2. , 6. , 0. , 0. , 1. , 2. , 1. , 0. ])
629
+ rvalues = np .array ([0. , 2. , 3. , 4. , 0. , 0. , 1. , 3. , 2. , 0. ])
630
+
631
+ a = SparseArray (values , kind = kind , fill_value = 0 )
632
+ b = SparseArray (rvalues , kind = kind , fill_value = 0 )
633
+ self ._check_comparison_ops (a , b , values , rvalues )
634
+
561
635
def test_float_array (self ):
562
636
values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
563
637
rvalues = np .array ([2 , np .nan , 2 , 3 , np .nan , 0 , 1 , 5 , 2 , np .nan ])
@@ -601,6 +675,28 @@ def test_float_array_different_kind(self):
601
675
b = SparseArray (rvalues , kind = 'block' , fill_value = 2 )
602
676
self ._check_numeric_ops (a , b , values , rvalues )
603
677
678
+ def test_float_array_comparison (self ):
679
+ values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
680
+ rvalues = np .array ([2 , np .nan , 2 , 3 , np .nan , 0 , 1 , 5 , 2 , np .nan ])
681
+
682
+ for kind in ['integer' , 'block' ]:
683
+ a = SparseArray (values , kind = kind )
684
+ b = SparseArray (rvalues , kind = kind )
685
+ self ._check_comparison_ops (a , b , values , rvalues )
686
+ self ._check_comparison_ops (a , b * 0 , values , rvalues * 0 )
687
+
688
+ a = SparseArray (values , kind = kind , fill_value = 0 )
689
+ b = SparseArray (rvalues , kind = kind )
690
+ self ._check_comparison_ops (a , b , values , rvalues )
691
+
692
+ a = SparseArray (values , kind = kind , fill_value = 0 )
693
+ b = SparseArray (rvalues , kind = kind , fill_value = 0 )
694
+ self ._check_comparison_ops (a , b , values , rvalues )
695
+
696
+ a = SparseArray (values , kind = kind , fill_value = 1 )
697
+ b = SparseArray (rvalues , kind = kind , fill_value = 2 )
698
+ self ._check_comparison_ops (a , b , values , rvalues )
699
+
604
700
605
701
if __name__ == '__main__' :
606
702
import nose
0 commit comments