Skip to content

Commit 5f86351

Browse files
authored
Allow str to be passing to rolling() window and use IntervalClosedType literal for series as well (#385)
* Allow str to be passed to rolling() window parameter and re-use IntervalClosedType literal for series as well * Add VSCode project settings folder to .gitignore
1 parent 617529a commit 5f86351

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ venv.bak/
120120
# PyCharm project settings
121121
.idea/
122122

123+
# VSCode project settings
124+
.vscode/
125+
123126
# mkdocs documentation
124127
/site
125128

pandas-stubs/core/frame.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ class DataFrame(NDFrame, OpsMixin):
17761776
@overload
17771777
def rolling(
17781778
self,
1779-
window: int | BaseOffset | BaseIndexer,
1779+
window: int | str | BaseOffset | BaseIndexer,
17801780
min_periods: int | None = ...,
17811781
center: _bool = ...,
17821782
*,
@@ -1790,7 +1790,7 @@ class DataFrame(NDFrame, OpsMixin):
17901790
@overload
17911791
def rolling(
17921792
self,
1793-
window: int | BaseOffset | BaseIndexer,
1793+
window: int | str | BaseOffset | BaseIndexer,
17941794
min_periods: int | None = ...,
17951795
center: _bool = ...,
17961796
*,

pandas-stubs/core/series.pyi

+5-4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ from pandas._typing import (
8686
HashableT3,
8787
IgnoreRaise,
8888
IndexingInt,
89+
IntervalClosedType,
8990
JoinHow,
9091
JsonSeriesOrient,
9192
Level,
@@ -1574,28 +1575,28 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
15741575
@overload
15751576
def rolling(
15761577
self,
1577-
window: int | BaseOffset | BaseIndexer,
1578+
window: int | _str | BaseOffset | BaseIndexer,
15781579
min_periods: int | None = ...,
15791580
center: _bool = ...,
15801581
*,
15811582
win_type: _str,
15821583
on: _str | None = ...,
15831584
axis: SeriesAxisType = ...,
1584-
closed: _str | None = ...,
1585+
closed: IntervalClosedType | None = ...,
15851586
step: int | None = ...,
15861587
method: CalculationMethod = ...,
15871588
) -> Window[Series]: ...
15881589
@overload
15891590
def rolling(
15901591
self,
1591-
window: int | BaseOffset | BaseIndexer,
1592+
window: int | _str | BaseOffset | BaseIndexer,
15921593
min_periods: int | None = ...,
15931594
center: _bool = ...,
15941595
*,
15951596
win_type: None = ...,
15961597
on: _str | None = ...,
15971598
axis: SeriesAxisType = ...,
1598-
closed: _str | None = ...,
1599+
closed: IntervalClosedType | None = ...,
15991600
step: int | None = ...,
16001601
method: CalculationMethod = ...,
16011602
) -> Rolling[Series]: ...

tests/test_windowing.py

+20
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313

1414
from tests import check
1515

16+
from pandas.tseries.frequencies import to_offset
17+
1618
IDX = date_range("1/1/2000", periods=700, freq="D")
1719
S = Series(np.random.standard_normal(700))
1820
DF = DataFrame({"col1": S, "col2": S})
21+
S_DTI = Series(data=np.random.standard_normal(700), index=IDX)
22+
DF_DTI = DataFrame(data=np.random.standard_normal(700), index=IDX)
1923

2024

2125
def test_rolling_basic() -> None:
@@ -43,6 +47,22 @@ def test_rolling_basic_math() -> None:
4347
check(assert_type(DF.rolling(10).rank("max"), DataFrame), DataFrame)
4448

4549

50+
def test_rolling_datetime_index() -> None:
51+
offset_1d = to_offset("1D")
52+
assert offset_1d is not None
53+
54+
check(assert_type(DF_DTI.rolling("1D"), "Rolling[DataFrame]"), Rolling, DataFrame)
55+
check(
56+
assert_type(DF_DTI.rolling(offset_1d), "Rolling[DataFrame]"), Rolling, DataFrame
57+
)
58+
check(assert_type(S_DTI.rolling("1D"), "Rolling[Series]"), Rolling, Series)
59+
check(
60+
assert_type(S_DTI.rolling(offset_1d), "Rolling[Series]"),
61+
Rolling,
62+
Series,
63+
)
64+
65+
4666
def test_rolling_apply() -> None:
4767
check(assert_type(DF.rolling(10).apply(np.mean), DataFrame), DataFrame)
4868

0 commit comments

Comments
 (0)