@@ -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,57 @@ 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
+ # Test with different dtypes
5775
+ for dtype in [int , float ]:
5776
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5777
+ 'B' : range (3 , 9 )}).astype (dtype )
5778
+ result = df .groupby ('A' ).cummin ()
5779
+ expected = pd .DataFrame ({'B' : [3 , 3 , 3 , 6 , 6 , 6 ]}).astype (dtype )
5780
+ tm .assert_frame_equal (result , expected )
5781
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5782
+ tm .assert_frame_equal (result , expected )
5783
+
5784
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5785
+ 'B' : range (8 , 2 , - 1 )}).astype (dtype )
5786
+ result = df .groupby ('A' ).cummax ()
5787
+ expected = pd .DataFrame ({'B' : [8 , 8 , 8 , 5 , 5 , 5 ]}).astype (dtype )
5788
+ tm .assert_frame_equal (result , expected )
5789
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5790
+ tm .assert_frame_equal (result , expected )
5791
+
5792
+ # Some entries in column are nan
5793
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5794
+ 'B' : range (3 , 9 )})
5795
+ df .loc [[1 , 4 ], 'B' ] = np .nan
5796
+ result = df .groupby ('A' ).cummin ()
5797
+ expected = pd .DataFrame ({'B' : [3 , np .nan , 3 , 6 , np .nan , 6 ]})
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
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5803
+ 'B' : range (8 , 2 , - 1 )})
5804
+ df .loc [[1 , 4 ], 'B' ] = np .nan
5805
+ result = df .groupby ('A' ).cummax ()
5806
+ expected = pd .DataFrame ({'B' : [8 , np .nan , 8 , 5 , np .nan , 5 ]})
5807
+ tm .assert_frame_equal (result , expected )
5808
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5809
+ tm .assert_frame_equal (result , expected )
5810
+
5811
+ # Entire column is nan
5812
+ df ['B' ] = np .nan
5813
+ expected = pd .DataFrame ({'B' : [np .nan ] * 6 })
5814
+ result = df .groupby ('A' ).cummin ()
5815
+ tm .assert_frame_equal (expected , result )
5816
+ result = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5817
+ tm .assert_frame_equal (expected , result )
5818
+ result = df .groupby ('A' ).cummax ()
5819
+ tm .assert_frame_equal (expected , result )
5820
+ result = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5821
+ tm .assert_frame_equal (expected , result )
5822
+
5780
5823
5781
5824
def assert_fp_equal (a , b ):
5782
5825
assert (np .abs (a - b ) < 1e-12 ).all ()
0 commit comments