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 8 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
7 changes: 4 additions & 3 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2513,8 +2513,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()
11 changes: 11 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,17 @@ 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_000_000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use a smaller number here

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
26 changes: 9 additions & 17 deletions pandas/tests/window/test_timeseries_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ 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 @@ -664,17 +666,14 @@ def test_groupby_monotonic(self):
df = DataFrame(data=data, columns=["name", "date", "amount"])
df["date"] = to_datetime(df["date"])

expected = (
df.set_index("date")
.groupby("name")
.apply(lambda x: x.rolling("180D")["amount"].sum())
)
result = df.groupby("name").rolling("180D", on="date")["amount"].sum()
tm.assert_series_equal(result, expected)
with pytest.raises(ValueError, match=r".* must be monotonic"):
df.groupby("name").rolling("180D", on="date")

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

# superceded by 43909

dates = date_range(start="2016-01-01 09:30:00", periods=20, freq="s")
df = DataFrame(
{
Expand All @@ -684,15 +683,8 @@ 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)

df2 = df.sort_values("B")
result = df2.groupby("A").rolling("4s", on="B").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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add back the .sort_values(...) then a successful groupby / rolling

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls do this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the successful groupby with rolling is the test above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but its not the same as what you are doing. it fails, you sort then it works. this needs testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed - amended in last push


def test_rolling_cov_offset(self):
# GH16058
Expand Down