@@ -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,81 @@ 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 [np .int32 , np .float32 , np .float64 ]:
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
+ # Test min value for dtype
5784
+ try :
5785
+ min_val = np .iinfo (dtype ).min
5786
+ except ValueError :
5787
+ min_val = np .finfo (dtype ).min
5788
+ df .loc [[1 , 4 ], 'B' ] = min_val
5789
+ result = df .groupby ('A' ).cummin ()
5790
+ expected = pd .DataFrame ({'B' : [3 , min_val , min_val ,
5791
+ 6 , min_val , min_val ]}).astype (dtype )
5792
+ tm .assert_frame_equal (result , expected )
5793
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5794
+ tm .assert_frame_equal (result , expected )
5795
+
5796
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5797
+ 'B' : range (8 , 2 , - 1 )}).astype (dtype )
5798
+ result = df .groupby ('A' ).cummax ()
5799
+ expected = pd .DataFrame ({'B' : [8 , 8 , 8 , 5 , 5 , 5 ]}).astype (dtype )
5800
+ tm .assert_frame_equal (result , expected )
5801
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5802
+ tm .assert_frame_equal (result , expected )
5803
+ # Test max value for dtype
5804
+ try :
5805
+ max_val = np .iinfo (dtype ).max
5806
+ except ValueError :
5807
+ max_val = np .finfo (dtype ).max
5808
+ df .loc [[1 , 4 ], 'B' ] = max_val
5809
+ result = df .groupby ('A' ).cummax ()
5810
+ expected = pd .DataFrame ({'B' : [8 , max_val , max_val ,
5811
+ 5 , max_val , max_val ]}).astype (dtype )
5812
+ tm .assert_frame_equal (result , expected )
5813
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5814
+ tm .assert_frame_equal (result , expected )
5815
+
5816
+ # Some entries in column are nan
5817
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5818
+ 'B' : range (3 , 9 )})
5819
+ df .loc [[1 , 4 ], 'B' ] = np .nan
5820
+ result = df .groupby ('A' ).cummin ()
5821
+ expected = pd .DataFrame ({'B' : [3 , np .nan , 3 , 6 , np .nan , 6 ]})
5822
+ tm .assert_frame_equal (result , expected )
5823
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5824
+ tm .assert_frame_equal (result , expected )
5825
+
5826
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 2 , 2 , 2 ],
5827
+ 'B' : range (8 , 2 , - 1 )})
5828
+ df .loc [[1 , 4 ], 'B' ] = np .nan
5829
+ result = df .groupby ('A' ).cummax ()
5830
+ expected = pd .DataFrame ({'B' : [8 , np .nan , 8 , 5 , np .nan , 5 ]})
5831
+ tm .assert_frame_equal (result , expected )
5832
+ expected = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5833
+ tm .assert_frame_equal (result , expected )
5834
+
5835
+ # Entire column is nan
5836
+ df ['B' ] = np .nan
5837
+ expected = pd .DataFrame ({'B' : [np .nan ] * 6 })
5838
+ result = df .groupby ('A' ).cummin ()
5839
+ tm .assert_frame_equal (expected , result )
5840
+ result = df .groupby ('A' ).B .apply (lambda x : x .cummin ()).to_frame ()
5841
+ tm .assert_frame_equal (expected , result )
5842
+ result = df .groupby ('A' ).cummax ()
5843
+ tm .assert_frame_equal (expected , result )
5844
+ result = df .groupby ('A' ).B .apply (lambda x : x .cummax ()).to_frame ()
5845
+ tm .assert_frame_equal (expected , result )
5846
+
5780
5847
5781
5848
def assert_fp_equal (a , b ):
5782
5849
assert (np .abs (a - b ) < 1e-12 ).all ()
0 commit comments