@@ -698,39 +698,87 @@ def test_numpy_compat(func):
698
698
@pytest .mark .xfail (
699
699
_is_numpy_dev , reason = "https://github.com/pandas-dev/pandas/issues/31992"
700
700
)
701
- def test_cummin_cummax (dtype , min_val , max_val ):
701
+ def test_cummin (dtype , min_val , max_val ):
702
702
# GH 15048
703
703
base_df = pd .DataFrame (
704
704
{"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]}
705
705
)
706
706
expected_mins = [3 , 3 , 3 , 2 , 2 , 2 , 2 , 1 ]
707
- expected_maxs = [3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 ]
708
707
709
708
df = base_df .astype (dtype )
710
709
711
- # cummin
712
710
expected = pd .DataFrame ({"B" : expected_mins }).astype (dtype )
713
711
result = df .groupby ("A" ).cummin ()
714
712
tm .assert_frame_equal (result , expected )
715
713
result = df .groupby ("A" ).B .apply (lambda x : x .cummin ()).to_frame ()
716
714
tm .assert_frame_equal (result , expected )
717
715
718
- # Test cummin w/ min value for dtype
716
+ # Test w/ min value for dtype
719
717
df .loc [[2 , 6 ], "B" ] = min_val
720
718
expected .loc [[2 , 3 , 6 , 7 ], "B" ] = min_val
721
719
result = df .groupby ("A" ).cummin ()
722
720
tm .assert_frame_equal (result , expected )
723
721
expected = df .groupby ("A" ).B .apply (lambda x : x .cummin ()).to_frame ()
724
722
tm .assert_frame_equal (result , expected )
725
723
726
- # cummax
724
+ # Test nan in some values
725
+ base_df .loc [[0 , 2 , 4 , 6 ], "B" ] = np .nan
726
+ expected = pd .DataFrame ({"B" : [np .nan , 4 , np .nan , 2 , np .nan , 3 , np .nan , 1 ]})
727
+ result = base_df .groupby ("A" ).cummin ()
728
+ tm .assert_frame_equal (result , expected )
729
+ expected = base_df .groupby ("A" ).B .apply (lambda x : x .cummin ()).to_frame ()
730
+ tm .assert_frame_equal (result , expected )
731
+
732
+ # Test nan in entire column
733
+ base_df ["B" ] = np .nan
734
+ expected = pd .DataFrame ({"B" : [np .nan ] * 8 })
735
+ result = base_df .groupby ("A" ).cummin ()
736
+ tm .assert_frame_equal (expected , result )
737
+ result = base_df .groupby ("A" ).B .apply (lambda x : x .cummin ()).to_frame ()
738
+ tm .assert_frame_equal (expected , result )
739
+
740
+ # GH 15561
741
+ df = pd .DataFrame (dict (a = [1 ], b = pd .to_datetime (["2001" ])))
742
+ expected = pd .Series (pd .to_datetime ("2001" ), index = [0 ], name = "b" )
743
+
744
+ result = df .groupby ("a" )["b" ].cummin ()
745
+ tm .assert_series_equal (expected , result )
746
+
747
+ # GH 15635
748
+ df = pd .DataFrame (dict (a = [1 , 2 , 1 ], b = [1 , 2 , 2 ]))
749
+ result = df .groupby ("a" ).b .cummin ()
750
+ expected = pd .Series ([1 , 2 , 1 ], name = "b" )
751
+ tm .assert_series_equal (result , expected )
752
+
753
+
754
+ @pytest .mark .parametrize (
755
+ "dtype, min_val, max_val" ,
756
+ [
757
+ (np .int32 , np .iinfo (np .int32 ).min , np .iinfo (np .int32 ).max ),
758
+ (np .int64 , np .iinfo (np .int64 ).min , np .iinfo (np .int64 ).max ),
759
+ (np .float32 , np .finfo (np .float32 ).min , np .finfo (np .float32 ).max ),
760
+ (np .float64 , np .finfo (np .float64 ).min , np .finfo (np .float64 ).max ),
761
+ ],
762
+ )
763
+ @pytest .mark .xfail (
764
+ _is_numpy_dev , reason = "https://github.com/pandas-dev/pandas/issues/31992"
765
+ )
766
+ def test_cummax (dtype , min_val , max_val ):
767
+ # GH 15048
768
+ base_df = pd .DataFrame (
769
+ {"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]}
770
+ )
771
+ expected_maxs = [3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 ]
772
+
773
+ df = base_df .astype (dtype )
774
+
727
775
expected = pd .DataFrame ({"B" : expected_maxs }).astype (dtype )
728
776
result = df .groupby ("A" ).cummax ()
729
777
tm .assert_frame_equal (result , expected )
730
778
result = df .groupby ("A" ).B .apply (lambda x : x .cummax ()).to_frame ()
731
779
tm .assert_frame_equal (result , expected )
732
780
733
- # Test cummax w/ max value for dtype
781
+ # Test w/ max value for dtype
734
782
df .loc [[2 , 6 ], "B" ] = max_val
735
783
expected .loc [[2 , 3 , 6 , 7 ], "B" ] = max_val
736
784
result = df .groupby ("A" ).cummax ()
@@ -740,12 +788,6 @@ def test_cummin_cummax(dtype, min_val, max_val):
740
788
741
789
# Test nan in some values
742
790
base_df .loc [[0 , 2 , 4 , 6 ], "B" ] = np .nan
743
- expected = pd .DataFrame ({"B" : [np .nan , 4 , np .nan , 2 , np .nan , 3 , np .nan , 1 ]})
744
- result = base_df .groupby ("A" ).cummin ()
745
- tm .assert_frame_equal (result , expected )
746
- expected = base_df .groupby ("A" ).B .apply (lambda x : x .cummin ()).to_frame ()
747
- tm .assert_frame_equal (result , expected )
748
-
749
791
expected = pd .DataFrame ({"B" : [np .nan , 4 , np .nan , 4 , np .nan , 3 , np .nan , 3 ]})
750
792
result = base_df .groupby ("A" ).cummax ()
751
793
tm .assert_frame_equal (result , expected )
@@ -755,10 +797,6 @@ def test_cummin_cummax(dtype, min_val, max_val):
755
797
# Test nan in entire column
756
798
base_df ["B" ] = np .nan
757
799
expected = pd .DataFrame ({"B" : [np .nan ] * 8 })
758
- result = base_df .groupby ("A" ).cummin ()
759
- tm .assert_frame_equal (expected , result )
760
- result = base_df .groupby ("A" ).B .apply (lambda x : x .cummin ()).to_frame ()
761
- tm .assert_frame_equal (expected , result )
762
800
result = base_df .groupby ("A" ).cummax ()
763
801
tm .assert_frame_equal (expected , result )
764
802
result = base_df .groupby ("A" ).B .apply (lambda x : x .cummax ()).to_frame ()
@@ -767,21 +805,16 @@ def test_cummin_cummax(dtype, min_val, max_val):
767
805
# GH 15561
768
806
df = pd .DataFrame (dict (a = [1 ], b = pd .to_datetime (["2001" ])))
769
807
expected = pd .Series (pd .to_datetime ("2001" ), index = [0 ], name = "b" )
770
- for method in [ "cummax" , "cummin" ]:
771
- result = getattr ( df .groupby ("a" )["b" ], method ) ()
772
- tm .assert_series_equal (expected , result )
808
+
809
+ result = df .groupby ("a" )["b" ]. cummax ()
810
+ tm .assert_series_equal (expected , result )
773
811
774
812
# GH 15635
775
813
df = pd .DataFrame (dict (a = [1 , 2 , 1 ], b = [2 , 1 , 1 ]))
776
814
result = df .groupby ("a" ).b .cummax ()
777
815
expected = pd .Series ([2 , 1 , 2 ], name = "b" )
778
816
tm .assert_series_equal (result , expected )
779
817
780
- df = pd .DataFrame (dict (a = [1 , 2 , 1 ], b = [1 , 2 , 2 ]))
781
- result = df .groupby ("a" ).b .cummin ()
782
- expected = pd .Series ([1 , 2 , 1 ], name = "b" )
783
- tm .assert_series_equal (result , expected )
784
-
785
818
786
819
@pytest .mark .parametrize (
787
820
"in_vals, out_vals" ,
0 commit comments