@@ -688,46 +688,57 @@ def test_numpy_sum(self):
688
688
SparseArray (data ), out = out )
689
689
690
690
def test_cumsum (self ):
691
- data = np .arange (10 ).astype (float )
692
- out = SparseArray (data ).cumsum ()
693
- expected = SparseArray (data .cumsum ())
694
- tm .assert_sp_array_equal (out , expected )
691
+ non_null_data = np .array ([1 , 2 , 3 , 4 , 5 ], dtype = float )
692
+ non_null_expected = SparseArray (non_null_data .cumsum ())
695
693
696
- # TODO: gh-12855 - return a SparseArray here
697
- data [5 ] = np .nan
698
- out = SparseArray (data , fill_value = 2 ).cumsum ()
699
- self .assertNotIsInstance (out , SparseArray )
700
- tm .assert_numpy_array_equal (out , data .cumsum ())
694
+ null_data = np .array ([1 , 2 , np .nan , 4 , 5 ], dtype = float )
695
+ null_expected = SparseArray (np .array ([1.0 , 3.0 , np .nan , 7.0 , 12.0 ]))
696
+
697
+ for data , expected in [
698
+ (null_data , null_expected ),
699
+ (non_null_data , non_null_expected )
700
+ ]:
701
+ out = SparseArray (data ).cumsum ()
702
+ tm .assert_sp_array_equal (out , expected )
703
+
704
+ out = SparseArray (data , fill_value = np .nan ).cumsum ()
705
+ tm .assert_sp_array_equal (out , expected )
701
706
702
- out = SparseArray (data , fill_value = np .nan ).cumsum ()
703
- expected = SparseArray (np .array ([
704
- 0 , 1 , 3 , 6 , 10 , np .nan , 16 , 23 , 31 , 40 ]))
705
- tm .assert_sp_array_equal (out , expected )
707
+ out = SparseArray (data , fill_value = 2 ).cumsum ()
708
+ tm .assert_sp_array_equal (out , expected )
709
+
710
+ axis = 1 # SparseArray currently 1-D, so only axis = 0 is valid.
711
+ msg = "axis\(={axis}\) out of bounds" .format (axis = axis )
712
+ with tm .assertRaisesRegexp (ValueError , msg ):
713
+ SparseArray (data ).cumsum (axis = axis )
706
714
707
715
def test_numpy_cumsum (self ):
708
- data = np .arange (10 ).astype (float )
709
- out = np .cumsum (SparseArray (data ))
710
- expected = SparseArray (data .cumsum ())
711
- tm .assert_sp_array_equal (out , expected )
716
+ non_null_data = np .array ([1 , 2 , 3 , 4 , 5 ], dtype = float )
717
+ non_null_expected = SparseArray (non_null_data .cumsum ())
712
718
713
- # TODO: gh-12855 - return a SparseArray here
714
- data [5 ] = np .nan
715
- out = np .cumsum (SparseArray (data , fill_value = 2 ))
716
- self .assertNotIsInstance (out , SparseArray )
717
- tm .assert_numpy_array_equal (out , data .cumsum ())
719
+ null_data = np .array ([1 , 2 , np .nan , 4 , 5 ], dtype = float )
720
+ null_expected = SparseArray (np .array ([1.0 , 3.0 , np .nan , 7.0 , 12.0 ]))
718
721
719
- out = np .cumsum (SparseArray (data , fill_value = np .nan ))
720
- expected = SparseArray (np .array ([
721
- 0 , 1 , 3 , 6 , 10 , np .nan , 16 , 23 , 31 , 40 ]))
722
- tm .assert_sp_array_equal (out , expected )
722
+ for data , expected in [
723
+ (null_data , null_expected ),
724
+ (non_null_data , non_null_expected )
725
+ ]:
726
+ out = np .cumsum (SparseArray (data ))
727
+ tm .assert_sp_array_equal (out , expected )
723
728
724
- msg = "the 'dtype' parameter is not supported"
725
- tm .assertRaisesRegexp (ValueError , msg , np .cumsum ,
726
- SparseArray (data ), dtype = np .int64 )
729
+ out = np .cumsum (SparseArray (data , fill_value = np .nan ))
730
+ tm .assert_sp_array_equal (out , expected )
727
731
728
- msg = "the 'out' parameter is not supported"
729
- tm .assertRaisesRegexp (ValueError , msg , np .cumsum ,
730
- SparseArray (data ), out = out )
732
+ out = np .cumsum (SparseArray (data , fill_value = 2 ))
733
+ tm .assert_sp_array_equal (out , expected )
734
+
735
+ msg = "the 'dtype' parameter is not supported"
736
+ tm .assertRaisesRegexp (ValueError , msg , np .cumsum ,
737
+ SparseArray (data ), dtype = np .int64 )
738
+
739
+ msg = "the 'out' parameter is not supported"
740
+ tm .assertRaisesRegexp (ValueError , msg , np .cumsum ,
741
+ SparseArray (data ), out = out )
731
742
732
743
def test_mean (self ):
733
744
data = np .arange (10 ).astype (float )
0 commit comments