@@ -3701,128 +3701,68 @@ def test_cummin_cummax(self):
3701
3701
expected = pd .Series ([1 , 2 , 1 ], name = 'b' )
3702
3702
tm .assert_series_equal (result , expected )
3703
3703
3704
- def test_is_increasing_is_decreasing (self ):
3705
- # GH 17015
3706
-
3704
+ @pytest .mark .parametrize ('in_vals, out_vals' , [
3707
3705
# Basics: strictly increasing (T), strictly decreasing (F),
3708
3706
# abs val increasing (F), non-strictly increasing (T)
3709
- source_dict = {
3710
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3711
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3712
- 'C' : [1 , 2 , 5 , 3 , 2 , 0 , 4 , 5 , - 6 , 1 , 1 ]}
3713
- df = pd .DataFrame (source_dict )
3714
- result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3715
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3716
- data = [True , False , False , True ],
3717
- name = 'C' )
3718
- expected .index .name = 'B'
3719
- tm .assert_series_equal (result , expected )
3720
- # Also check result equal to manually taking x.is_monotonic_increasing.
3721
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3722
- tm .assert_series_equal (result , expected )
3723
-
3707
+ ([1 , 2 , 5 , 3 , 2 , 0 , 4 , 5 , - 6 , 1 , 1 ],
3708
+ [True , False , False , True ]),
3724
3709
# Test with inf vals
3725
- source_dict = {
3726
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3727
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3728
- 'C' : [1 , 2.1 , np .inf , 3 , 2 , np .inf , - np .inf , 5 , 11 , 1 , - np .inf ]}
3729
- expected .index .name = 'B'
3730
- df = pd .DataFrame (source_dict )
3731
- result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3732
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3733
- data = [True , False , True , False ],
3734
- name = 'C' )
3735
- expected .index .name = 'B'
3736
- tm .assert_series_equal (result , expected )
3737
- # Also check result equal to manually taking x.is_monotonic_increasing.
3738
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3739
- tm .assert_series_equal (result , expected )
3740
-
3710
+ ([1 , 2.1 , np .inf , 3 , 2 , np .inf , - np .inf , 5 , 11 , 1 , - np .inf ],
3711
+ [True , False , True , False ]),
3741
3712
# Test with nan vals; should always be False
3713
+ ([1 , 2 , np .nan , 3 , 2 , np .nan , np .nan , 5 , - np .inf , 1 , np .nan ],
3714
+ [False , False , False , False ]),
3715
+ ])
3716
+ def test_is_monotonic_increasing (self , in_vals , out_vals ):
3717
+ # GH 17015
3742
3718
source_dict = {
3743
3719
'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3744
3720
'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3745
- 'C' : [ 1 , 2 , np . nan , 3 , 2 , np . nan , np . nan , 5 , - np . inf , 1 , np . nan ] }
3721
+ 'C' : in_vals }
3746
3722
df = pd .DataFrame (source_dict )
3747
3723
result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3748
3724
expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3749
- data = [ False , False , False , False ] ,
3725
+ data = out_vals ,
3750
3726
name = 'C' )
3751
3727
expected .index .name = 'B'
3752
3728
tm .assert_series_equal (result , expected )
3753
- # Also check result equal to manually taking x.is_monotonic_increasing.
3754
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3755
- tm .assert_series_equal (result , expected )
3756
3729
3757
- # Test with single member groups; should be True except for np.nan
3758
- source_dict = {
3759
- 'A' : ['1' , '2' , '3' , '4' ],
3760
- 'B' : ['a' , 'b' , 'c' , 'd' ],
3761
- 'C' : [1 , 2 , np .nan , np .inf ]}
3762
- df = pd .DataFrame (source_dict )
3763
- result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3764
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3765
- data = [True , True , False , True ],
3766
- name = 'C' )
3767
- expected .index .name = 'B'
3768
- expected .index .name = 'B'
3769
- tm .assert_series_equal (result , expected )
3770
3730
# Also check result equal to manually taking x.is_monotonic_increasing.
3771
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3731
+ expected = (
3732
+ df .groupby (['B' ]).C .apply (lambda x : x .is_monotonic_increasing ))
3772
3733
tm .assert_series_equal (result , expected )
3773
3734
3774
- # As above, for .is_monotonic_decreasing()
3735
+ @ pytest . mark . parametrize ( 'in_vals, out_vals' , [
3775
3736
# Basics: strictly decreasing (T), strictly increasing (F),
3776
3737
# abs val decreasing (F), non-strictly increasing (T)
3777
- source_dict = {
3778
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3779
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3780
- 'C' : [10 , 9 , 7 , 3 , 4 , 5 , - 3 , 2 , 0 , 1 , 1 ]}
3781
- df = pd .DataFrame (source_dict )
3782
- result = df .groupby (['B' ]).C .is_monotonic_decreasing ()
3783
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3784
- data = [True , False , False , True ],
3785
- name = 'C' )
3786
- expected .index .name = 'B'
3787
- tm .assert_series_equal (result , expected )
3788
- # Also check result equal to manually taking x.is_monotonic_decreasing.
3789
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_decreasing )
3790
- tm .assert_series_equal (result , expected )
3791
-
3738
+ ([10 , 9 , 7 , 3 , 4 , 5 , - 3 , 2 , 0 , 1 , 1 ],
3739
+ [True , False , False , True ]),
3792
3740
# Test with inf vals
3793
- source_dict = {
3794
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3795
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3796
- 'C' : [np .inf , 1 , - np .inf , np .inf , 2 , - 3 , - np .inf , 5 , - 3 , - np .inf ,
3797
- - np .inf ]}
3798
- df = pd .DataFrame (source_dict )
3799
- result = df .groupby (['B' ]).C .is_monotonic_decreasing ()
3800
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3801
- data = [True , True , False , True ],
3802
- name = 'C' )
3803
- expected .index .name = 'B'
3804
- tm .assert_series_equal (result , expected )
3805
- # Also check result equal to manually taking x.is_monotonic_decreasing.
3806
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_decreasing )
3807
- tm .assert_series_equal (result , expected )
3808
-
3741
+ ([np .inf , 1 , - np .inf , np .inf , 2 , - 3 , - np .inf , 5 , - 3 , - np .inf , - np .inf ],
3742
+ [True , True , False , True ]),
3809
3743
# Test with nan vals; should always be False
3744
+ ([1 , 2 , np .nan , 3 , 2 , np .nan , np .nan , 5 , - np .inf , 1 , np .nan ],
3745
+ [False , False , False , False ]),
3746
+ ])
3747
+ def test_is_monotonic_decreasing (self , in_vals , out_vals ):
3748
+ # GH 17015
3810
3749
source_dict = {
3811
3750
'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3812
3751
'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3813
- 'C' : [1 , 2 , np .nan , 3 , 2 , np .nan , np .nan , 5 , - np .inf , 1 , np .nan ]}
3752
+ 'C' : in_vals }
3753
+
3814
3754
df = pd .DataFrame (source_dict )
3815
- result = df .groupby ([ 'B' ] ).C .is_monotonic_decreasing ()
3755
+ result = df .groupby ('B' ).C .is_monotonic_decreasing ()
3816
3756
expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3817
- data = [ False , False , False , False ] ,
3757
+ data = out_vals ,
3818
3758
name = 'C' )
3819
3759
expected .index .name = 'B'
3820
3760
tm .assert_series_equal (result , expected )
3761
+
3821
3762
# Also check result equal to manually taking x.is_monotonic_decreasing.
3822
3763
expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_decreasing )
3823
3764
tm .assert_series_equal (result , expected )
3824
3765
3825
-
3826
3766
def test_apply_numeric_coercion_when_datetime (self ):
3827
3767
# In the past, group-by/apply operations have been over-eager
3828
3768
# in converting dtypes to numeric, in the presence of datetime
0 commit comments