Skip to content

Commit 5a30ee4

Browse files
No-Streamjreback
authored andcommitted
added tests for gb.is_monotonically_increasing()/decreasing
1 parent 53e5a2b commit 5a30ee4

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+4
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ Various enhancements
324324
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`).
325325
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`)
326326
<<<<<<< HEAD
327+
<<<<<<< HEAD
327328

328329
- Improved the import time of pandas by about 2.25x. (:issue:`16764`)
329330
- Support for `PEP 519 -- Adding a file system path protocol
@@ -350,6 +351,9 @@ Various enhancements
350351
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (:issue:`17367`)
351352
=======
352353
- :func: groupby.is_monotonic_increasing and .is_monotonic_decreasing extend Series.is_monotonic_increasing to groups, returning whether each group is monotonically increasing or decreasing, respectively. (:issue:`17015`)
354+
=======
355+
- is_monotonic_increasing/decreasing is added to .groupby(). (:issue:`17015`)
356+
>>>>>>> 740c7c2... added tests for gb.is_monotonically_increasing()/decreasing
353357

354358
>>>>>>> e99897c... ENH: gb.is_monotonic_increasing, is_monotonic_decreasing #17015
355359

pandas/tests/groupby/test_groupby.py

+124
Original file line numberDiff line numberDiff line change
@@ -3701,6 +3701,7 @@ def test_cummin_cummax(self):
37013701
expected = pd.Series([1, 2, 1], name='b')
37023702
tm.assert_series_equal(result, expected)
37033703

3704+
<<<<<<< HEAD
37043705
@pytest.mark.parametrize('in_vals, out_vals', [
37053706
# Basics: strictly increasing (T), strictly decreasing (F),
37063707
# abs val increasing (F), non-strictly increasing (T)
@@ -3759,10 +3760,133 @@ def test_is_monotonic_decreasing(self, in_vals, out_vals):
37593760
expected.index.name = 'B'
37603761
tm.assert_series_equal(result, expected)
37613762

3763+
=======
3764+
def test_is_increasing_is_decreasing(self):
3765+
# GH 17015
3766+
3767+
# Basics: strictly increasing (T), strictly decreasing (F),
3768+
# abs val increasing (F), non-strictly increasing (T)
3769+
source_dict = {
3770+
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
3771+
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
3772+
'C': [1, 2, 5, 3, 2, 0, 4, 5, -6, 1, 1]}
3773+
df = pd.DataFrame(source_dict)
3774+
result = df.groupby(['B']).C.is_monotonic_increasing()
3775+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3776+
data=[True, False, False, True],
3777+
name='C')
3778+
expected.index.name = 'B'
3779+
tm.assert_series_equal(result, expected)
3780+
# Also check result equal to manually taking x.is_monotonic_increasing.
3781+
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_increasing)
3782+
tm.assert_series_equal(result, expected)
3783+
3784+
# Test with inf vals
3785+
source_dict = {
3786+
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
3787+
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
3788+
'C': [1, 2.1, np.inf, 3, 2, np.inf, -np.inf, 5, 11, 1, -np.inf]}
3789+
expected.index.name = 'B'
3790+
df = pd.DataFrame(source_dict)
3791+
result = df.groupby(['B']).C.is_monotonic_increasing()
3792+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3793+
data=[True, False, True, False],
3794+
name='C')
3795+
expected.index.name = 'B'
3796+
tm.assert_series_equal(result, expected)
3797+
# Also check result equal to manually taking x.is_monotonic_increasing.
3798+
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_increasing)
3799+
tm.assert_series_equal(result, expected)
3800+
3801+
# Test with nan vals; should always be False
3802+
source_dict = {
3803+
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
3804+
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
3805+
'C': [1, 2, np.nan, 3, 2, np.nan, np.nan, 5, -np.inf, 1, np.nan]}
3806+
df = pd.DataFrame(source_dict)
3807+
result = df.groupby(['B']).C.is_monotonic_increasing()
3808+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3809+
data=[False, False, False, False],
3810+
name='C')
3811+
expected.index.name = 'B'
3812+
tm.assert_series_equal(result, expected)
3813+
# Also check result equal to manually taking x.is_monotonic_increasing.
3814+
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_increasing)
3815+
tm.assert_series_equal(result, expected)
3816+
3817+
# Test with single member groups; should be True except for np.nan
3818+
source_dict = {
3819+
'A': ['1', '2', '3', '4'],
3820+
'B': ['a', 'b', 'c', 'd'],
3821+
'C': [1, 2, np.nan, np.inf]}
3822+
df = pd.DataFrame(source_dict)
3823+
result = df.groupby(['B']).C.is_monotonic_increasing()
3824+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3825+
data=[True, True, False, True],
3826+
name='C')
3827+
expected.index.name = 'B'
3828+
expected.index.name = 'B'
3829+
tm.assert_series_equal(result, expected)
3830+
# Also check result equal to manually taking x.is_monotonic_increasing.
3831+
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_increasing)
3832+
tm.assert_series_equal(result, expected)
3833+
3834+
# As above, for .is_monotonic_decreasing()
3835+
# Basics: strictly decreasing (T), strictly increasing (F),
3836+
# abs val decreasing (F), non-strictly increasing (T)
3837+
source_dict = {
3838+
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
3839+
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
3840+
'C': [10, 9, 7, 3, 4, 5, -3, 2, 0, 1, 1]}
3841+
df = pd.DataFrame(source_dict)
3842+
result = df.groupby(['B']).C.is_monotonic_decreasing()
3843+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3844+
data=[True, False, False, True],
3845+
name='C')
3846+
expected.index.name = 'B'
3847+
tm.assert_series_equal(result, expected)
37623848
# Also check result equal to manually taking x.is_monotonic_decreasing.
37633849
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_decreasing)
37643850
tm.assert_series_equal(result, expected)
37653851

3852+
# Test with inf vals
3853+
source_dict = {
3854+
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
3855+
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
3856+
'C': [np.inf, 1, -np.inf, np.inf, 2, -3, -np.inf, 5, -3, -np.inf,
3857+
-np.inf]}
3858+
df = pd.DataFrame(source_dict)
3859+
result = df.groupby(['B']).C.is_monotonic_decreasing()
3860+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3861+
data=[True, True, False, True],
3862+
name='C')
3863+
expected.index.name = 'B'
3864+
tm.assert_series_equal(result, expected)
3865+
# Also check result equal to manually taking x.is_monotonic_decreasing.
3866+
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_decreasing)
3867+
tm.assert_series_equal(result, expected)
3868+
3869+
# Test with nan vals; should always be False
3870+
source_dict = {
3871+
'A': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
3872+
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
3873+
'C': [1, 2, np.nan, 3, 2, np.nan, np.nan, 5, -np.inf, 1, np.nan]}
3874+
df = pd.DataFrame(source_dict)
3875+
result = df.groupby(['B']).C.is_monotonic_decreasing()
3876+
expected = pd.Series(index=['a', 'b', 'c', 'd'],
3877+
data=[False, False, False, False],
3878+
name='C')
3879+
expected.index.name = 'B'
3880+
tm.assert_series_equal(result, expected)
3881+
>>>>>>> 740c7c2... added tests for gb.is_monotonically_increasing()/decreasing
3882+
# Also check result equal to manually taking x.is_monotonic_decreasing.
3883+
expected = df.groupby('B').C.apply(lambda x: x.is_monotonic_decreasing)
3884+
tm.assert_series_equal(result, expected)
3885+
3886+
<<<<<<< HEAD
3887+
=======
3888+
3889+
>>>>>>> 740c7c2... added tests for gb.is_monotonically_increasing()/decreasing
37663890
def test_apply_numeric_coercion_when_datetime(self):
37673891
# In the past, group-by/apply operations have been over-eager
37683892
# in converting dtypes to numeric, in the presence of datetime

0 commit comments

Comments
 (0)