@@ -829,6 +829,52 @@ def test_numpy_mean(self):
829
829
tm .assertRaisesRegexp (ValueError , msg , np .mean ,
830
830
SparseArray (data ), out = out )
831
831
832
+ def test_ufunc (self ):
833
+ # GH 13853 make sure ufunc is applied to fill_value
834
+ sparse = SparseArray ([1 , np .nan , 2 , np .nan , - 2 ])
835
+ result = SparseArray ([1 , np .nan , 2 , np .nan , 2 ])
836
+ tm .assert_sp_array_equal (abs (sparse ), result )
837
+ tm .assert_sp_array_equal (np .abs (sparse ), result )
838
+
839
+ sparse = SparseArray ([1 , - 1 , 2 , - 2 ], fill_value = 1 )
840
+ result = SparseArray ([1 , 2 , 2 ], sparse_index = sparse .sp_index ,
841
+ fill_value = 1 )
842
+ tm .assert_sp_array_equal (abs (sparse ), result )
843
+ tm .assert_sp_array_equal (np .abs (sparse ), result )
844
+
845
+ sparse = SparseArray ([1 , - 1 , 2 , - 2 ], fill_value = - 1 )
846
+ result = SparseArray ([1 , 2 , 2 ], sparse_index = sparse .sp_index ,
847
+ fill_value = 1 )
848
+ tm .assert_sp_array_equal (abs (sparse ), result )
849
+ tm .assert_sp_array_equal (np .abs (sparse ), result )
850
+
851
+ sparse = SparseArray ([1 , np .nan , 2 , np .nan , - 2 ])
852
+ result = SparseArray (np .sin ([1 , np .nan , 2 , np .nan , - 2 ]))
853
+ tm .assert_sp_array_equal (np .sin (sparse ), result )
854
+
855
+ sparse = SparseArray ([1 , - 1 , 2 , - 2 ], fill_value = 1 )
856
+ result = SparseArray (np .sin ([1 , - 1 , 2 , - 2 ]), fill_value = np .sin (1 ))
857
+ tm .assert_sp_array_equal (np .sin (sparse ), result )
858
+
859
+ sparse = SparseArray ([1 , - 1 , 0 , - 2 ], fill_value = 0 )
860
+ result = SparseArray (np .sin ([1 , - 1 , 0 , - 2 ]), fill_value = np .sin (0 ))
861
+ tm .assert_sp_array_equal (np .sin (sparse ), result )
862
+
863
+ def test_ufunc_args (self ):
864
+ # GH 13853 make sure ufunc is applied to fill_value, including its arg
865
+ sparse = SparseArray ([1 , np .nan , 2 , np .nan , - 2 ])
866
+ result = SparseArray ([2 , np .nan , 3 , np .nan , - 1 ])
867
+ tm .assert_sp_array_equal (np .add (sparse , 1 ), result )
868
+
869
+ sparse = SparseArray ([1 , - 1 , 2 , - 2 ], fill_value = 1 )
870
+ result = SparseArray ([2 , 0 , 3 , - 1 ], fill_value = 2 )
871
+ tm .assert_sp_array_equal (np .add (sparse , 1 ), result )
872
+
873
+ sparse = SparseArray ([1 , - 1 , 0 , - 2 ], fill_value = 0 )
874
+ result = SparseArray ([2 , 0 , 1 , - 1 ], fill_value = 1 )
875
+ tm .assert_sp_array_equal (np .add (sparse , 1 ), result )
876
+
877
+
832
878
if __name__ == '__main__' :
833
879
import nose
834
880
nose .runmodule (argv = [__file__ , '-vvs' , '-x' , '--pdb' , '--pdb-failure' ],
0 commit comments