Skip to content

ENH: Add step to rolling #332

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 3 commits into from
Sep 27, 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
2 changes: 2 additions & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,6 @@ class StyleExportDict(TypedDict, total=False):
hide_column_names: bool
css: dict[str, str | int]

CalculationMethod = Literal["single", "table"]

__all__ = ["npt", "type_t"]
13 changes: 10 additions & 3 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ from pandas.core.groupby.generic import (
_DataFrameGroupByScalar,
)
from pandas.core.groupby.grouper import Grouper
from pandas.core.indexers import BaseIndexer
from pandas.core.indexes.base import Index
from pandas.core.indexing import (
_iLocIndexer,
Expand All @@ -46,6 +47,7 @@ from pandas.core.window.rolling import (
import xarray as xr

from pandas._libs.missing import NAType
from pandas._libs.tslibs import BaseOffset
from pandas._typing import (
S1,
AggFuncTypeBase,
Expand All @@ -56,6 +58,7 @@ from pandas._typing import (
Axes,
Axis,
AxisType,
CalculationMethod,
ColspaceArgType,
CompressionOptions,
Dtype,
Expand Down Expand Up @@ -1429,7 +1432,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
min_periods: int = ...,
axis: AxisType = ...,
method: Literal["single", "table"] = ...,
method: CalculationMethod = ...,
) -> Expanding[DataFrame]: ...
@overload
def ffill(
Expand Down Expand Up @@ -1763,26 +1766,30 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def rolling(
self,
window,
window: int | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
win_type: _str,
on: Hashable | None = ...,
axis: AxisType = ...,
closed: IntervalClosedType | None = ...,
step: int | None = ...,
method: CalculationMethod = ...,
) -> Window[DataFrame]: ...
@overload
def rolling(
self,
window,
window: int | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
win_type: None = ...,
on: Hashable | None = ...,
axis: AxisType = ...,
closed: IntervalClosedType | None = ...,
step: int | None = ...,
method: CalculationMethod = ...,
) -> Rolling[DataFrame]: ...
def rpow(
self,
Expand Down
13 changes: 10 additions & 3 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ from pandas.core.groupby.generic import (
_SeriesGroupByNonScalar,
_SeriesGroupByScalar,
)
from pandas.core.indexers import BaseIndexer
from pandas.core.indexes.accessors import (
CombinedDatetimelikeProperties,
PeriodProperties,
Expand Down Expand Up @@ -62,6 +63,7 @@ from pandas.core.window.rolling import (
import xarray as xr

from pandas._libs.missing import NAType
from pandas._libs.tslibs import BaseOffset
from pandas._typing import (
S1,
AggFuncTypeBase,
Expand All @@ -71,6 +73,7 @@ from pandas._typing import (
Axes,
Axis,
AxisType,
CalculationMethod,
CompressionOptions,
DtypeObj,
FilePathOrBuffer,
Expand Down Expand Up @@ -1318,7 +1321,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
self,
min_periods: int = ...,
axis: SeriesAxisType = ...,
method: Literal["single", "table"] = ...,
method: CalculationMethod = ...,
) -> Expanding[Series]: ...
def floordiv(
self,
Expand Down Expand Up @@ -1496,26 +1499,30 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
@overload
def rolling(
self,
window,
window: int | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
win_type: _str,
on: _str | None = ...,
axis: SeriesAxisType = ...,
closed: _str | None = ...,
step: int | None = ...,
method: CalculationMethod = ...,
) -> Window[Series]: ...
@overload
def rolling(
self,
window,
window: int | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
*,
win_type: None = ...,
on: _str | None = ...,
axis: SeriesAxisType = ...,
closed: _str | None = ...,
step: int | None = ...,
method: CalculationMethod = ...,
) -> Rolling[Series]: ...
def rpow(
self,
Expand Down
4 changes: 2 additions & 2 deletions pandas-stubs/core/window/ewm.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import (
Any,
Generic,
Literal,
overload,
)

Expand All @@ -17,6 +16,7 @@ from pandas._typing import (
AggFuncTypeFrame,
AggFuncTypeSeriesToFrame,
Axis,
CalculationMethod,
NDFrameT,
TimedeltaConvertibleTypes,
WindowingEngine,
Expand All @@ -36,7 +36,7 @@ class ExponentialMovingWindow(BaseWindow[NDFrameT], Generic[NDFrameT]):
ignore_na: bool = ...,
axis: Axis = ...,
times: str | np.ndarray | Series | None = ...,
method: Literal["single", "table"] = ...,
method: CalculationMethod = ...,
) -> None: ...
@overload
def aggregate(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_windowing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
from pandas import (
DataFrame,
Series,
Expand Down Expand Up @@ -304,3 +305,35 @@ def test_ewm_aggregate_series() -> None:
DataFrame,
)
check(assert_type(S.ewm(span=10).agg("sum"), Series), Series)


def test_rolling_step_method() -> None:
check(
assert_type(DF.rolling(10, step=5, method="single"), "Rolling[DataFrame]"),
Rolling,
)
check(assert_type(DF.rolling(10, method="table"), "Rolling[DataFrame]"), Rolling)


def test_rolling_window() -> None:
df_time = pd.DataFrame(
{"B": [0, 1, 2, np.nan, 4]},
index=[
pd.Timestamp("20130101 09:00:00"),
pd.Timestamp("20130101 09:00:02"),
pd.Timestamp("20130101 09:00:03"),
pd.Timestamp("20130101 09:00:05"),
pd.Timestamp("20130101 09:00:06"),
],
)

indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2)
check(
assert_type(df_time.rolling(window=indexer, min_periods=1).sum(), DataFrame),
DataFrame,
)
s = df_time.iloc[:, 0]
check(
assert_type(s.rolling(window=indexer, min_periods=1).sum(), Series),
Series,
)