@@ -4977,10 +4977,6 @@ def test_groupby_whitelist(self):
4977
4977
'max' ,
4978
4978
'head' ,
4979
4979
'tail' ,
4980
- 'cumsum' ,
4981
- 'cumprod' ,
4982
- 'cummin' ,
4983
- 'cummax' ,
4984
4980
'cumcount' ,
4985
4981
'resample' ,
4986
4982
'describe' ,
@@ -5018,10 +5014,6 @@ def test_groupby_whitelist(self):
5018
5014
'max' ,
5019
5015
'head' ,
5020
5016
'tail' ,
5021
- 'cumsum' ,
5022
- 'cumprod' ,
5023
- 'cummin' ,
5024
- 'cummax' ,
5025
5017
'cumcount' ,
5026
5018
'resample' ,
5027
5019
'describe' ,
@@ -5777,6 +5769,85 @@ def test_agg_over_numpy_arrays(self):
5777
5769
5778
5770
assert_frame_equal (result , expected )
5779
5771
5772
+ def test_cummin_cummax (self ):
5773
+ # GH 15048
5774
+ num_types = [np .int32 , np .int64 , np .float32 , np .float64 ]
5775
+ num_mins = [np .iinfo (np .int32 ).min , np .iinfo (np .int64 ).min ,
5776
+ np .finfo (np .float32 ).min , np .finfo (np .float64 ).min ]
5777
+ num_max = [np .iinfo (np .int32 ).max , np .iinfo (np .int64 ).max ,
5778
+ np .finfo (np .float32 ).max , np .finfo (np .float64 ).max ]
5779
+ base_df = pd .DataFrame ({'A' : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ],
5780
+ 'B' : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
5781
+ expected_mins = [3 , 3 , 3 , 2 , 2 , 2 , 2 , 1 ]
5782
+ expected_maxs = [3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 ]
5783
+
5784
+ for dtype , min_val , max_val in zip (num_types , num_mins , num_max ):
5785
+ df = base_df .astype (dtype )
5786
+
5787
+ # cummin
5788
+ expected = pd .DataFrame ({'B' : expected_mins }).astype (dtype )
5789
+ result = df .groupby ('A' ).cummin ()
5790
+ tm .assert_frame_equal (result , expected )
5791
+ result = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5792
+ tm .assert_frame_equal (result , expected )
5793
+
5794
+ # Test cummin w/ min value for dtype
5795
+ df .loc [[2 , 6 ], 'B' ] = min_val
5796
+ expected .loc [[2 , 3 , 6 , 7 ], 'B' ] = min_val
5797
+ result = df .groupby ('A' ).cummin ()
5798
+ tm .assert_frame_equal (result , expected )
5799
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5800
+ tm .assert_frame_equal (result , expected )
5801
+
5802
+ # cummax
5803
+ expected = pd .DataFrame ({'B' : expected_maxs }).astype (dtype )
5804
+ result = df .groupby ('A' ).cummax ()
5805
+ tm .assert_frame_equal (result , expected )
5806
+ result = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5807
+ tm .assert_frame_equal (result , expected )
5808
+
5809
+ # Test cummax w/ max value for dtype
5810
+ df .loc [[2 , 6 ], 'B' ] = max_val
5811
+ expected .loc [[2 , 3 , 6 , 7 ], 'B' ] = max_val
5812
+ result = df .groupby ('A' ).cummax ()
5813
+ tm .assert_frame_equal (result , expected )
5814
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5815
+ tm .assert_frame_equal (result , expected )
5816
+
5817
+ # Test nan in some values
5818
+ base_df .loc [[0 , 2 , 4 , 6 ], 'B' ] = np .nan
5819
+ expected = pd .DataFrame ({'B' : [np .nan , 4 , np .nan , 2 ,
5820
+ np .nan , 3 , np .nan , 1 ]})
5821
+ result = base_df .groupby ('A' ).cummin ()
5822
+ tm .assert_frame_equal (result , expected )
5823
+ expected = (base_df .groupby ('A' )
5824
+ .B
5825
+ .apply (lambda x : x .cummin ())
5826
+ .to_frame ())
5827
+ tm .assert_frame_equal (result , expected )
5828
+
5829
+ expected = pd .DataFrame ({'B' : [np .nan , 4 , np .nan , 4 ,
5830
+ np .nan , 3 , np .nan , 3 ]})
5831
+ result = base_df .groupby ('A' ).cummax ()
5832
+ tm .assert_frame_equal (result , expected )
5833
+ expected = (base_df .groupby ('A' )
5834
+ .B
5835
+ .apply (lambda x : x .cummax ())
5836
+ .to_frame ())
5837
+ tm .assert_frame_equal (result , expected )
5838
+
5839
+ # Test nan in entire column
5840
+ base_df ['B' ] = np .nan
5841
+ expected = pd .DataFrame ({'B' : [np .nan ] * 8 })
5842
+ result = base_df .groupby ('A' ).cummin ()
5843
+ tm .assert_frame_equal (expected , result )
5844
+ result = base_df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5845
+ tm .assert_frame_equal (expected , result )
5846
+ result = base_df .groupby ('A' ).cummax ()
5847
+ tm .assert_frame_equal (expected , result )
5848
+ result = base_df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5849
+ tm .assert_frame_equal (expected , result )
5850
+
5780
5851
5781
5852
def assert_fp_equal (a , b ):
5782
5853
assert (np .abs (a - b ) < 1e-12 ).all ()
0 commit comments