Skip to content

Commit f8554ee

Browse files
committed
parametrized tests for gb.is_monotonic_increasing/decreasing
1 parent 740c7c2 commit f8554ee

File tree

2 files changed

+31
-89
lines changed

2 files changed

+31
-89
lines changed

doc/source/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,8 @@ The following methods are available only for ``SeriesGroupBy`` objects.
17801780
SeriesGroupBy.nunique
17811781
SeriesGroupBy.unique
17821782
SeriesGroupBy.value_counts
1783+
SeriesGroupBy.is_monotonic_increasing
1784+
SeriesGroupBy.is_monotonic_decreasing
17831785

17841786
The following methods are available only for ``DataFrameGroupBy`` objects.
17851787

pandas/tests/groupby/test_groupby.py

Lines changed: 29 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,128 +3843,68 @@ def test_cummin_cummax(self):
38433843
expected = pd.Series([1, 2, 1], name='b')
38443844
tm.assert_series_equal(result, expected)
38453845

3846-
def test_is_increasing_is_decreasing(self):
3847-
# GH 17015
3848-
3846+
@pytest.mark.parametrize('in_vals, out_vals', [
38493847
# Basics: strictly increasing (T), strictly decreasing (F),
38503848
# 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]),
38663851
# 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]),
38833854
# 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
38843860
source_dict = {
38853861
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
38863862
'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}
38883864
df = pd.DataFrame(source_dict)
38893865
result = df.groupby(['B']).C.is_monotonic_increasing()
38903866
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3891-
data=[False, False, False, False],
3867+
data=out_vals,
38923868
name='C')
38933869
expected.index.name = 'B'
38943870
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)
38983871

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)
39123872
# 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))
39143875
tm.assert_series_equal(result, expected)
39153876

3916-
# As above, for .is_monotonic_decreasing()
3877+
@pytest.mark.parametrize('in_vals, out_vals', [
39173878
# Basics: strictly decreasing (T), strictly increasing (F),
39183879
# 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]),
39343882
# 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]),
39513885
# 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
39523891
source_dict = {
39533892
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
39543893
'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+
39563896
df = pd.DataFrame(source_dict)
3957-
result = df.groupby(['B']).C.is_monotonic_decreasing()
3897+
result = df.groupby('B').C.is_monotonic_decreasing()
39583898
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3959-
data=[False, False, False, False],
3899+
data=out_vals,
39603900
name='C')
39613901
expected.index.name = 'B'
39623902
tm.assert_series_equal(result, expected)
3903+
39633904
# Also check result equal to manually taking x.is_monotonic_decreasing.
39643905
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_decreasing)
39653906
tm.assert_series_equal(result, expected)
39663907

3967-
39683908
def test_apply_numeric_coercion_when_datetime(self):
39693909
# In the past, group-by/apply operations have been over-eager
39703910
# in converting dtypes to numeric, in the presence of datetime

0 commit comments

Comments
 (0)