Skip to content

Commit 58b7b65

Browse files
phoflJulianWgs
authored andcommitted
BUG: Raise ValueError with nan in timeaware windows (pandas-dev#36822)
1 parent a60ef2c commit 58b7b65

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ Groupby/resample/rolling
460460
- Bug in :meth:`DataFrameGroupBy.ffill` and :meth:`DataFrameGroupBy.bfill` where a ``NaN`` group would return filled values instead of ``NaN`` when ``dropna=True`` (:issue:`34725`)
461461
- Bug in :meth:`RollingGroupby.count` where a ``ValueError`` was raised when specifying the ``closed`` parameter (:issue:`35869`)
462462
- Bug in :meth:`DataFrame.groupby.rolling` returning wrong values with partial centered window (:issue:`36040`).
463+
- Bug in :meth:`DataFrameGroupBy.rolling` returned wrong values with timeaware window containing ``NaN``. Raises ``ValueError`` because windows are not monotonic now (:issue:`34617`)
463464

464465
Reshaping
465466
^^^^^^^^^

pandas/core/window/rolling.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -2085,10 +2085,13 @@ def _validate_monotonic(self):
20852085
Validate monotonic (increasing or decreasing).
20862086
"""
20872087
if not (self._on.is_monotonic_increasing or self._on.is_monotonic_decreasing):
2088-
formatted = self.on
2089-
if self.on is None:
2090-
formatted = "index"
2091-
raise ValueError(f"{formatted} must be monotonic")
2088+
self._raise_monotonic_error()
2089+
2090+
def _raise_monotonic_error(self):
2091+
formatted = self.on
2092+
if self.on is None:
2093+
formatted = "index"
2094+
raise ValueError(f"{formatted} must be monotonic")
20922095

20932096
def _validate_freq(self):
20942097
"""
@@ -2323,3 +2326,12 @@ def _get_window_indexer(self, window: int) -> GroupbyIndexer:
23232326
indexer_kwargs=indexer_kwargs,
23242327
)
23252328
return window_indexer
2329+
2330+
def _validate_monotonic(self):
2331+
"""
2332+
Validate that on is monotonic;
2333+
in this case we have to check only for nans, because
2334+
monotonicy was already validated at a higher level.
2335+
"""
2336+
if self._on.hasnans:
2337+
self._raise_monotonic_error()

pandas/tests/window/test_grouper.py

+17
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,20 @@ def test_groupby_rolling_sem(self, func, kwargs):
549549
),
550550
)
551551
tm.assert_frame_equal(result, expected)
552+
553+
@pytest.mark.parametrize(
554+
("rollings", "key"), [({"on": "a"}, "a"), ({"on": None}, "index")]
555+
)
556+
def test_groupby_rolling_nans_in_index(self, rollings, key):
557+
# GH: 34617
558+
df = pd.DataFrame(
559+
{
560+
"a": pd.to_datetime(["2020-06-01 12:00", "2020-06-01 14:00", np.nan]),
561+
"b": [1, 2, 3],
562+
"c": [1, 1, 1],
563+
}
564+
)
565+
if key == "index":
566+
df = df.set_index("a")
567+
with pytest.raises(ValueError, match=f"{key} must be monotonic"):
568+
df.groupby("c").rolling("60min", **rollings)

0 commit comments

Comments
 (0)