@@ -3843,128 +3843,68 @@ def test_cummin_cummax(self):
3843
3843
expected = pd .Series ([1 , 2 , 1 ], name = 'b' )
3844
3844
tm .assert_series_equal (result , expected )
3845
3845
3846
- def test_is_increasing_is_decreasing (self ):
3847
- # GH 17015
3848
-
3846
+ @pytest .mark .parametrize ('in_vals, out_vals' , [
3849
3847
# Basics: strictly increasing (T), strictly decreasing (F),
3850
3848
# abs val increasing (F), non-strictly increasing (T)
3851
- source_dict = {
3852
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3853
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3854
- 'C' : [1 , 2 , 5 , 3 , 2 , 0 , 4 , 5 , - 6 , 1 , 1 ]}
3855
- df = pd .DataFrame (source_dict )
3856
- result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3857
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3858
- data = [True , False , False , True ],
3859
- name = 'C' )
3860
- expected .index .name = 'B'
3861
- tm .assert_series_equal (result , expected )
3862
- # Also check result equal to manually taking x.is_monotonic_increasing.
3863
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3864
- tm .assert_series_equal (result , expected )
3865
-
3849
+ ([1 , 2 , 5 , 3 , 2 , 0 , 4 , 5 , - 6 , 1 , 1 ],
3850
+ [True , False , False , True ]),
3866
3851
# Test with inf vals
3867
- source_dict = {
3868
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3869
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3870
- 'C' : [1 , 2.1 , np .inf , 3 , 2 , np .inf , - np .inf , 5 , 11 , 1 , - np .inf ]}
3871
- expected .index .name = 'B'
3872
- df = pd .DataFrame (source_dict )
3873
- result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3874
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3875
- data = [True , False , True , False ],
3876
- name = 'C' )
3877
- expected .index .name = 'B'
3878
- tm .assert_series_equal (result , expected )
3879
- # Also check result equal to manually taking x.is_monotonic_increasing.
3880
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3881
- tm .assert_series_equal (result , expected )
3882
-
3852
+ ([1 , 2.1 , np .inf , 3 , 2 , np .inf , - np .inf , 5 , 11 , 1 , - np .inf ],
3853
+ [True , False , True , False ]),
3883
3854
# Test with nan vals; should always be False
3855
+ ([1 , 2 , np .nan , 3 , 2 , np .nan , np .nan , 5 , - np .inf , 1 , np .nan ],
3856
+ [False , False , False , False ]),
3857
+ ])
3858
+ def test_is_monotonic_increasing (self , in_vals , out_vals ):
3859
+ # GH 17015
3884
3860
source_dict = {
3885
3861
'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3886
3862
'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3887
- 'C' : [ 1 , 2 , np . nan , 3 , 2 , np . nan , np . nan , 5 , - np . inf , 1 , np . nan ] }
3863
+ 'C' : in_vals }
3888
3864
df = pd .DataFrame (source_dict )
3889
3865
result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3890
3866
expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3891
- data = [ False , False , False , False ] ,
3867
+ data = out_vals ,
3892
3868
name = 'C' )
3893
3869
expected .index .name = 'B'
3894
3870
tm .assert_series_equal (result , expected )
3895
- # Also check result equal to manually taking x.is_monotonic_increasing.
3896
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3897
- tm .assert_series_equal (result , expected )
3898
3871
3899
- # Test with single member groups; should be True except for np.nan
3900
- source_dict = {
3901
- 'A' : ['1' , '2' , '3' , '4' ],
3902
- 'B' : ['a' , 'b' , 'c' , 'd' ],
3903
- 'C' : [1 , 2 , np .nan , np .inf ]}
3904
- df = pd .DataFrame (source_dict )
3905
- result = df .groupby (['B' ]).C .is_monotonic_increasing ()
3906
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3907
- data = [True , True , False , True ],
3908
- name = 'C' )
3909
- expected .index .name = 'B'
3910
- expected .index .name = 'B'
3911
- tm .assert_series_equal (result , expected )
3912
3872
# Also check result equal to manually taking x.is_monotonic_increasing.
3913
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_increasing )
3873
+ expected = (
3874
+ df .groupby (['B' ]).C .apply (lambda x : x .is_monotonic_increasing ))
3914
3875
tm .assert_series_equal (result , expected )
3915
3876
3916
- # As above, for .is_monotonic_decreasing()
3877
+ @ pytest . mark . parametrize ( 'in_vals, out_vals' , [
3917
3878
# Basics: strictly decreasing (T), strictly increasing (F),
3918
3879
# abs val decreasing (F), non-strictly increasing (T)
3919
- source_dict = {
3920
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3921
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3922
- 'C' : [10 , 9 , 7 , 3 , 4 , 5 , - 3 , 2 , 0 , 1 , 1 ]}
3923
- df = pd .DataFrame (source_dict )
3924
- result = df .groupby (['B' ]).C .is_monotonic_decreasing ()
3925
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3926
- data = [True , False , False , True ],
3927
- name = 'C' )
3928
- expected .index .name = 'B'
3929
- tm .assert_series_equal (result , expected )
3930
- # Also check result equal to manually taking x.is_monotonic_decreasing.
3931
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_decreasing )
3932
- tm .assert_series_equal (result , expected )
3933
-
3880
+ ([10 , 9 , 7 , 3 , 4 , 5 , - 3 , 2 , 0 , 1 , 1 ],
3881
+ [True , False , False , True ]),
3934
3882
# Test with inf vals
3935
- source_dict = {
3936
- 'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3937
- 'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3938
- 'C' : [np .inf , 1 , - np .inf , np .inf , 2 , - 3 , - np .inf , 5 , - 3 , - np .inf ,
3939
- - np .inf ]}
3940
- df = pd .DataFrame (source_dict )
3941
- result = df .groupby (['B' ]).C .is_monotonic_decreasing ()
3942
- expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3943
- data = [True , True , False , True ],
3944
- name = 'C' )
3945
- expected .index .name = 'B'
3946
- tm .assert_series_equal (result , expected )
3947
- # Also check result equal to manually taking x.is_monotonic_decreasing.
3948
- expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_decreasing )
3949
- tm .assert_series_equal (result , expected )
3950
-
3883
+ ([np .inf , 1 , - np .inf , np .inf , 2 , - 3 , - np .inf , 5 , - 3 , - np .inf , - np .inf ],
3884
+ [True , True , False , True ]),
3951
3885
# Test with nan vals; should always be False
3886
+ ([1 , 2 , np .nan , 3 , 2 , np .nan , np .nan , 5 , - np .inf , 1 , np .nan ],
3887
+ [False , False , False , False ]),
3888
+ ])
3889
+ def test_is_monotonic_decreasing (self , in_vals , out_vals ):
3890
+ # GH 17015
3952
3891
source_dict = {
3953
3892
'A' : ['1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ],
3954
3893
'B' : ['a' , 'a' , 'a' , 'b' , 'b' , 'b' , 'c' , 'c' , 'c' , 'd' , 'd' ],
3955
- 'C' : [1 , 2 , np .nan , 3 , 2 , np .nan , np .nan , 5 , - np .inf , 1 , np .nan ]}
3894
+ 'C' : in_vals }
3895
+
3956
3896
df = pd .DataFrame (source_dict )
3957
- result = df .groupby ([ 'B' ] ).C .is_monotonic_decreasing ()
3897
+ result = df .groupby ('B' ).C .is_monotonic_decreasing ()
3958
3898
expected = pd .Series (index = ['a' , 'b' , 'c' , 'd' ],
3959
- data = [ False , False , False , False ] ,
3899
+ data = out_vals ,
3960
3900
name = 'C' )
3961
3901
expected .index .name = 'B'
3962
3902
tm .assert_series_equal (result , expected )
3903
+
3963
3904
# Also check result equal to manually taking x.is_monotonic_decreasing.
3964
3905
expected = df .groupby ('B' ).C .apply (lambda x : x .is_monotonic_decreasing )
3965
3906
tm .assert_series_equal (result , expected )
3966
3907
3967
-
3968
3908
def test_apply_numeric_coercion_when_datetime (self ):
3969
3909
# In the past, group-by/apply operations have been over-eager
3970
3910
# in converting dtypes to numeric, in the presence of datetime
0 commit comments