Skip to content

"on" argument of DataFrame.rolling only works for datetime columns #21687

Closed
@bchu

Description

@bchu

the on= argument of DataFrame.rolling only works for datetime columns.

df = pd.DataFrame([
    [18, 0],
    [2, 0],
    [1, 0],
    [9, 1],
    [8, 1],
], columns=['value', 'roll'])
df.roll = pd.to_datetime(df.roll, unit='s')
df.rolling('1s', on='roll').value.max()

returns:

0    18.0
1    18.0
2    18.0
3     9.0
4     9.0
Name: value, dtype: float64

as expected.

But

df.rolling(1, on='roll').value.max()

returns:

0    18.0
1     2.0
2     1.0
3     9.0
4     8.0
Name: value, dtype: float64

If this is intentional behavior, I'd be happy to change the docs to note this (the docs currently imply that on= can be used for any column).

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffDocsUsage QuestionWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions