Skip to content

Commit 0491c11

Browse files
authored
Allow Timedelta for Series.rolling and DataFrame.rolling (#705)
* Add Timedelta annotation to .rolling * Allow datetime.timedelta as well * Reorder and indent tests
1 parent 70c412c commit 0491c11

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,7 @@ class DataFrame(NDFrame, OpsMixin):
19551955
@overload
19561956
def rolling(
19571957
self,
1958-
window: int | str | BaseOffset | BaseIndexer,
1958+
window: int | str | _dt.timedelta | BaseOffset | BaseIndexer,
19591959
min_periods: int | None = ...,
19601960
center: _bool = ...,
19611961
on: Hashable | None = ...,
@@ -1969,7 +1969,7 @@ class DataFrame(NDFrame, OpsMixin):
19691969
@overload
19701970
def rolling(
19711971
self,
1972-
window: int | str | BaseOffset | BaseIndexer,
1972+
window: int | str | _dt.timedelta | BaseOffset | BaseIndexer,
19731973
min_periods: int | None = ...,
19741974
center: _bool = ...,
19751975
on: Hashable | None = ...,

pandas-stubs/core/series.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
18341834
@overload
18351835
def rolling(
18361836
self,
1837-
window: int | _str | BaseOffset | BaseIndexer,
1837+
window: int | _str | timedelta | BaseOffset | BaseIndexer,
18381838
min_periods: int | None = ...,
18391839
center: _bool = ...,
18401840
on: _str | None = ...,
@@ -1848,7 +1848,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
18481848
@overload
18491849
def rolling(
18501850
self,
1851-
window: int | _str | BaseOffset | BaseIndexer,
1851+
window: int | _str | timedelta | BaseOffset | BaseIndexer,
18521852
min_periods: int | None = ...,
18531853
center: _bool = ...,
18541854
on: _str | None = ...,

tests/test_windowing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import datetime as dt
2+
13
import numpy as np
24
import pandas as pd
35
from pandas import (
46
DataFrame,
57
Series,
8+
Timedelta,
69
date_range,
710
)
811
from pandas.core.window import (
@@ -62,6 +65,14 @@ def test_rolling_datetime_index() -> None:
6265
Series,
6366
)
6467

68+
td = Timedelta("1D")
69+
check(assert_type(DF_DTI.rolling(td), "Rolling[DataFrame]"), Rolling, DataFrame)
70+
check(assert_type(S_DTI.rolling(td), "Rolling[Series]"), Rolling, Series)
71+
72+
dttd = dt.timedelta(days=1)
73+
check(assert_type(DF_DTI.rolling(dttd), "Rolling[DataFrame]"), Rolling, DataFrame)
74+
check(assert_type(S_DTI.rolling(dttd), "Rolling[Series]"), Rolling, Series)
75+
6576

6677
def test_rolling_apply() -> None:
6778
check(assert_type(DF.rolling(10).apply(np.mean), DataFrame), DataFrame)

0 commit comments

Comments
 (0)