@@ -262,6 +262,19 @@ def test_constructor_bool(self):
262262 self .assertEqual (dense .dtype , bool )
263263 tm .assert_numpy_array_equal (dense , data )
264264
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+
265278 def test_constructor_float32 (self ):
266279 # GH 10648
267280 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):
522535 tm .assert_numpy_array_equal ((a ** b ).to_dense (), a_dense ** b_dense )
523536 tm .assert_numpy_array_equal ((b ** a ).to_dense (), b_dense ** a_dense )
524537
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+
525563 def test_float_scalar (self ):
526564 values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
527565
@@ -541,6 +579,25 @@ def test_float_scalar(self):
541579 self ._check_numeric_ops (a , 0 , values , 0 )
542580 self ._check_numeric_ops (a , 3 , values , 3 )
543581
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+
544601 def test_float_same_index (self ):
545602 # when sp_index are the same
546603 for kind in ['integer' , 'block' ]:
@@ -558,6 +615,23 @@ def test_float_same_index(self):
558615 b = SparseArray (rvalues , kind = kind , fill_value = 0 )
559616 self ._check_numeric_ops (a , b , values , rvalues )
560617
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+
561635 def test_float_array (self ):
562636 values = np .array ([np .nan , 1 , 2 , 0 , np .nan , 0 , 1 , 2 , 1 , np .nan ])
563637 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):
601675 b = SparseArray (rvalues , kind = 'block' , fill_value = 2 )
602676 self ._check_numeric_ops (a , b , values , rvalues )
603677
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+
604700
605701if __name__ == '__main__' :
606702 import nose
0 commit comments