Skip to content

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

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

Closed
bchu opened this issue Jun 30, 2018 · 3 comments · Fixed by #27265
Closed

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

bchu opened this issue Jun 30, 2018 · 3 comments · Fixed by #27265
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Docs Usage Question Window rolling, ewma, expanding
Milestone

Comments

@bchu
Copy link

bchu commented Jun 30, 2018

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).

@gfyoung gfyoung added Usage Question Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Docs labels Jul 1, 2018
@gfyoung
Copy link
Member

gfyoung commented Jul 1, 2018

cc @jreback @jorisvandenbossche

@WillAyd WillAyd added the Window rolling, ewma, expanding label Sep 4, 2018
@WillAyd
Copy link
Member

WillAyd commented Jul 7, 2019

Is this just confusion over the size of the window being a frequency versus a fixed size? Both of these results look correct to me

@jreback
Copy link
Contributor

jreback commented Jul 8, 2019

looks ok to me. I think your confusion is that when you have an integer argument basically on doesn't do anything as the roll does not use the index.

In [9]: df.set_index('roll').rolling(2).value.sum()                                                                                                                                                                                                          
Out[9]: 
roll
0     NaN
0    20.0
0     3.0
1    10.0
1    17.0
Name: value, dtype: float64

In [10]: df.rolling(2, on='roll').value.sum()                                                                                                                                                                                                                
Out[10]: 
0     NaN
1    20.0
2     3.0
3    10.0
4    17.0
Name: value, dtype: float64

@jreback jreback added this to the 0.25.0 milestone Jul 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Docs Usage Question Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants