Skip to content

BUG: 43909 - check monoticity of rolling groupby #44068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0595567
BUG: 43909 - check monoticity of rolling groupby
jamesholcombe Oct 16, 2021
247e8f3
Fixes from pre-commit [automated commit]
jamesholcombe Oct 16, 2021
9b76c6c
Merge remote-tracking branch 'origin/master' into dev-bug
jamesholcombe Oct 17, 2021
d6dd13e
amended tests
jamesholcombe Oct 17, 2021
a5467ed
Fixes from pre-commit [automated commit]
jamesholcombe Oct 17, 2021
e234f9a
commit to go through checks again
jamesholcombe Oct 17, 2021
f962e27
Merge branch 'dev-bug' of https://github.com/jamesholcombe/pandas int…
jamesholcombe Oct 17, 2021
a743ff2
Fixes from pre-commit [automated commit]
jamesholcombe Oct 17, 2021
9f132a5
ammending as per comments
jamesholcombe Oct 18, 2021
29de8c7
Merge branch 'dev-bug' of https://github.com/jamesholcombe/pandas int…
jamesholcombe Oct 18, 2021
a4c22d1
Fixes from pre-commit [automated commit]
jamesholcombe Oct 18, 2021
195b254
readded succesful groupby on rolling test
jamesholcombe Oct 24, 2021
ffcf1c4
Merge branch 'master' of github.com:pandas-dev/pandas into dev-bug
jamesholcombe Oct 24, 2021
f08495d
merge
jamesholcombe Oct 24, 2021
cf41892
Fixes from pre-commit [automated commit]
jamesholcombe Oct 24, 2021
ebae655
added back successful gby test
jamesholcombe Oct 31, 2021
8163a16
Merge branch 'dev-bug' of https://github.com/jamesholcombe/pandas int…
jamesholcombe Oct 31, 2021
08ee1ec
Fixes from pre-commit [automated commit]
jamesholcombe Oct 31, 2021
791c183
Merge branch 'pandas-dev:master' into dev-bug
jamesholcombe Nov 1, 2021
cb73fa2
Merge branch 'master' of https://github.com/pandas-dev/pandas
jamesholcombe Dec 11, 2021
08ec776
Merge branch 'master' into dev-bug
jamesholcombe Dec 11, 2021
95975c3
code style
jamesholcombe Dec 11, 2021
52ea34b
DOC: add whatsnew note for issue 43909
jamesholcombe Dec 12, 2021
5fba5ce
Merge branch 'master' of https://github.com/pandas-dev/pandas
jamesholcombe Dec 22, 2021
737f446
merge master
jamesholcombe Dec 22, 2021
a3357a5
fix typo
jamesholcombe Dec 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.nth` failing on ``axis=1`` (:issue:`43926`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not respecting right bound on centered datetime-like windows, if the index contain duplicates (:issue:`3944`)
- Bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` when using a :class:`pandas.api.indexers.BaseIndexer` subclass that returned unequal start and end arrays would segfault instead of raising a ``ValueError`` (:issue:`44470`)
- Bug in :meth:`Groupby.rolling` when non-monotonic data passed, fails to correctly raise ``ValueError`` (:issue:`43909`)
- Fixed bug where grouping by a :class:`Series` that has a categorical data type and length unequal to the axis of grouping raised ``ValueError`` (:issue:`44179`)

Reshaping
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2626,8 +2626,9 @@ def _get_window_indexer(self) -> GroupbyIndexer:
def _validate_monotonic(self):
"""
Validate that on is monotonic;
in this case we have to check only for nans, because
monotonicity was already validated at a higher level.
"""
if self._on.hasnans:
if (
not (self._on.is_monotonic_increasing or self._on.is_monotonic_decreasing)
or self._on.hasnans
):
self._raise_monotonic_error()
12 changes: 12 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,18 @@ def test_groupby_rolling_nan_included():
tm.assert_frame_equal(result, expected)


def test_groupby_rolling_non_monotonic():
# GH 43909

shuffled = [3, 0, 1, 2]
sec = 1_000
df = DataFrame(
[{"t": Timestamp(2 * x * sec), "x": x + 1, "c": 42} for x in shuffled]
)
with pytest.raises(ValueError, match=r".* must be monotonic"):
df.groupby("c").rolling(on="t", window="3s")


@pytest.mark.parametrize("method", ["skew", "kurt"])
def test_rolling_skew_kurt_numerical_stability(method):
# GH#6929
Expand Down
14 changes: 11 additions & 3 deletions pandas/tests/window/test_timeseries_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ def test_groupby_monotonic(self):
# GH 15130
# we don't need to validate monotonicity when grouping

# GH 43909 we should raise an error here to match
# behaviour of non-groupby rolling.

data = [
["David", "1/1/2015", 100],
["David", "1/5/2015", 500],
Expand All @@ -663,6 +666,7 @@ def test_groupby_monotonic(self):

df = DataFrame(data=data, columns=["name", "date", "amount"])
df["date"] = to_datetime(df["date"])
df = df.sort_values("date")

expected = (
df.set_index("date")
Expand All @@ -672,9 +676,11 @@ def test_groupby_monotonic(self):
result = df.groupby("name").rolling("180D", on="date")["amount"].sum()
tm.assert_series_equal(result, expected)

def test_non_monotonic(self):
def test_non_monotonic_raises(self):
# GH 13966 (similar to #15130, closed by #15175)

# superseded by 43909

dates = date_range(start="2016-01-01 09:30:00", periods=20, freq="s")
df = DataFrame(
{
Expand All @@ -684,11 +690,13 @@ def test_non_monotonic(self):
}
)

result = df.groupby("A").rolling("4s", on="B").C.mean()
expected = (
df.set_index("B").groupby("A").apply(lambda x: x.rolling("4s")["C"].mean())
)
tm.assert_series_equal(result, expected)
with pytest.raises(ValueError, match=r".* must be monotonic"):
df.groupby("A").rolling(
"4s", on="B"
).C.mean() # should raise for non-monotonic t series

df2 = df.sort_values("B")
result = df2.groupby("A").rolling("4s", on="B").C.mean()
Expand Down