Skip to content
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

Feature request: first & last for rolling() #33155

Open
yohplala opened this issue Mar 30, 2020 · 9 comments · May be fixed by #60579
Open

Feature request: first & last for rolling() #33155

yohplala opened this issue Mar 30, 2020 · 9 comments · May be fixed by #60579
Assignees
Labels
Enhancement Window rolling, ewma, expanding

Comments

@yohplala
Copy link

yohplala commented Mar 30, 2020

Code Sample, a copy-pastable example if possible

# This works:
df1 = df.resample(to_freq,
                  closed='left',
                  kind='period',
                   ).agg(OrderedDict([('Open', 'first'),
                                      ('Close', 'last'),
                                                        ]))

# This doesn't:
df2 = df.rolling(my_indexer).agg(
                 OrderedDict([('Open', 'first'),
                              ('Close', 'last') ]))
>>> AttributeError: 'first' is not a valid function for 'Rolling' object

df3 = df.rolling(my_indexer).agg(
                 OrderedDict([
                              ('Close', 'last') ]))
>>> AttributeError: 'last' is not a valid function for 'Rolling' object

Please, could 'first' and 'last' be made available for rolling window?
Thanks for your support!
Bests,

@mroeschke mroeschke added Enhancement Window rolling, ewma, expanding labels Mar 30, 2020
@yohplala
Copy link
Author

yohplala commented Mar 31, 2020

Link to SO question, which provides some input data, as requested by commentators.

import pandas as pd
from random import seed
from random import randint
from collections import OrderedDict

# DataFrame
ts_1h = pd.date_range(start='2020-01-01 00:00+00:00', end='2020-01-02 00:00+00:00', freq='1h')
seed(1)
values = [randint(0,10) for ts in ts_1h]
df = pd.DataFrame({'Values' : values}, index=ts_1h)

# First & last work with resample
resampled_first = df.resample('3H',
                              closed='left',
                              kind='period',
                             ).agg(OrderedDict([('Values', 'first')]))
resampled_last = df.resample('3H',
                             closed='left',
                             kind='period',
                            ).agg(OrderedDict([('Values', 'last')]))

# They don't with rolling
rolling_first = df.rolling(3).agg(OrderedDict([('Values', 'first')]))
rolling_first = df.rolling(3).agg(OrderedDict([('Values', 'last')]))

@venkata-mi
Copy link

'take'

@venkata-mi
Copy link

I will take this issue

@soccerboys2008
Copy link

you will?

@venkata-mi
Copy link

is this still a relevant feature? @soccerboys2008

@thwblueboy
Copy link

It's a good feature for me to get the first/last non-nan element in specific rolling window (with min_periods=1).

@snitish
Copy link
Contributor

snitish commented Dec 15, 2024

What would "first" and "last" mean in the context of a rolling window? i.e. would first refer to the most recent element (it being the most significant element in a way) or the oldest element (going by the index, oldest is first)? Perhaps, to keep it consistent with Groupby, it should be the latter. @mroeschke

@thwblueboy
Copy link

What would "first" and "last" mean in the context of a rolling window? i.e. would first refer to the most recent element (it being the most significant element in a way) or the oldest element (going by the index, oldest is first)? Perhaps, to keep it consistent with Groupby, it should be the latter. @mroeschke

In my context, the latter (going by the index, oldest is first).

@snitish snitish linked a pull request Dec 16, 2024 that will close this issue
5 tasks
@snitish
Copy link
Contributor

snitish commented Dec 16, 2024

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants