Skip to content

Allow str to be passing to rolling() window and use IntervalClosedType literal for series as well #385

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 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ venv.bak/
# PyCharm project settings
.idea/

# VSCode project settings
.vscode/

# mkdocs documentation
/site

Expand Down
4 changes: 2 additions & 2 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def rolling(
self,
window: int | BaseOffset | BaseIndexer,
window: int | str | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
Expand All @@ -1790,7 +1790,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def rolling(
self,
window: int | BaseOffset | BaseIndexer,
window: int | str | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
Expand Down
9 changes: 5 additions & 4 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ from pandas._typing import (
HashableT3,
IgnoreRaise,
IndexingInt,
IntervalClosedType,
JoinHow,
JsonSeriesOrient,
Level,
Expand Down Expand Up @@ -1574,28 +1575,28 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
@overload
def rolling(
self,
window: int | BaseOffset | BaseIndexer,
window: int | _str | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
win_type: _str,
on: _str | None = ...,
axis: SeriesAxisType = ...,
closed: _str | None = ...,
closed: IntervalClosedType | None = ...,
step: int | None = ...,
method: CalculationMethod = ...,
) -> Window[Series]: ...
@overload
def rolling(
self,
window: int | BaseOffset | BaseIndexer,
window: int | _str | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
win_type: None = ...,
on: _str | None = ...,
axis: SeriesAxisType = ...,
closed: _str | None = ...,
closed: IntervalClosedType | None = ...,
step: int | None = ...,
method: CalculationMethod = ...,
) -> Rolling[Series]: ...
Expand Down
20 changes: 20 additions & 0 deletions tests/test_windowing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

from tests import check

from pandas.tseries.frequencies import to_offset

IDX = date_range("1/1/2000", periods=700, freq="D")
S = Series(np.random.standard_normal(700))
DF = DataFrame({"col1": S, "col2": S})
S_DTI = Series(data=np.random.standard_normal(700), index=IDX)
DF_DTI = DataFrame(data=np.random.standard_normal(700), index=IDX)


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


def test_rolling_datetime_index() -> None:
offset_1d = to_offset("1D")
assert offset_1d is not None

check(assert_type(DF_DTI.rolling("1D"), "Rolling[DataFrame]"), Rolling, DataFrame)
check(
assert_type(DF_DTI.rolling(offset_1d), "Rolling[DataFrame]"), Rolling, DataFrame
)
check(assert_type(S_DTI.rolling("1D"), "Rolling[Series]"), Rolling, Series)
check(
assert_type(S_DTI.rolling(offset_1d), "Rolling[Series]"),
Rolling,
Series,
)


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

Expand Down