Skip to content

Commit 7cf8470

Browse files
gfyoungMateusz Górski
authored and
Mateusz Górski
committed
TST: Test DataFrame.rolling with window as string (pandas-dev#29392)
1 parent 0e880c9 commit 7cf8470

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

pandas/tests/window/test_rolling.py

+59-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import timedelta
1+
from datetime import datetime, timedelta
22

33
import numpy as np
44
import pytest
@@ -7,7 +7,7 @@
77
import pandas.util._test_decorators as td
88

99
import pandas as pd
10-
from pandas import DataFrame, Series
10+
from pandas import DataFrame, Index, Series
1111
from pandas.core.window import Rolling
1212
from pandas.tests.window.common import Base
1313
import pandas.util.testing as tm
@@ -361,3 +361,60 @@ def test_rolling_datetime(self, axis_frame, tz_naive_fixture):
361361
}
362362
)
363363
tm.assert_frame_equal(result, expected)
364+
365+
366+
def test_rolling_window_as_string():
367+
# see gh-22590
368+
date_today = datetime.now()
369+
days = pd.date_range(date_today, date_today + timedelta(365), freq="D")
370+
371+
npr = np.random.RandomState(seed=421)
372+
373+
data = npr.randint(1, high=100, size=len(days))
374+
df = DataFrame({"DateCol": days, "metric": data})
375+
376+
df.set_index("DateCol", inplace=True)
377+
result = df.rolling(window="21D", min_periods=2, closed="left")["metric"].agg("max")
378+
379+
expData = (
380+
[np.nan] * 2
381+
+ [88.0] * 16
382+
+ [97.0] * 9
383+
+ [98.0]
384+
+ [99.0] * 21
385+
+ [95.0] * 16
386+
+ [93.0] * 5
387+
+ [89.0] * 5
388+
+ [96.0] * 21
389+
+ [94.0] * 14
390+
+ [90.0] * 13
391+
+ [88.0] * 2
392+
+ [90.0] * 9
393+
+ [96.0] * 21
394+
+ [95.0] * 6
395+
+ [91.0]
396+
+ [87.0] * 6
397+
+ [92.0] * 21
398+
+ [83.0] * 2
399+
+ [86.0] * 10
400+
+ [87.0] * 5
401+
+ [98.0] * 21
402+
+ [97.0] * 14
403+
+ [93.0] * 7
404+
+ [87.0] * 4
405+
+ [86.0] * 4
406+
+ [95.0] * 21
407+
+ [85.0] * 14
408+
+ [83.0] * 2
409+
+ [76.0] * 5
410+
+ [81.0] * 2
411+
+ [98.0] * 21
412+
+ [95.0] * 14
413+
+ [91.0] * 7
414+
+ [86.0]
415+
+ [93.0] * 3
416+
+ [95.0] * 20
417+
)
418+
419+
expected = Series(expData, index=Index(days, name="DateCol"), name="metric")
420+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)