Skip to content

API:How can I apply a function on rolling DataFrames #20919

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
zhangyw49 opened this issue May 2, 2018 · 2 comments
Closed

API:How can I apply a function on rolling DataFrames #20919

zhangyw49 opened this issue May 2, 2018 · 2 comments
Labels
API Design Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode Window rolling, ewma, expanding

Comments

@zhangyw49
Copy link

zhangyw49 commented May 2, 2018

well, my function's input is ndarray-like and it's output is a scaler. The question is I have no method to apply this function on rolling object.

from sklearn.decomposition import PCA

df = pd.DataFrame({"A": [1, 2, 3, 4, 5, 6, 7, 8], "B": [2, 3, 4, 5, 6, 7, 8, 9]})

def first_variance(X):
    pca = PCA(n_components=1)
    pca.fit(X)
    return pca.explained_variance_

df.rolling(4).apply(first_variance)

The domo is incorrect because the "apply" method only can apply by index or column.

@jreback
Copy link
Contributor

jreback commented May 2, 2018

this is a duplicate of #15095

@jreback jreback closed this as completed May 2, 2018
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode API Design Duplicate Report Duplicate issue or pull request labels May 2, 2018
@jreback jreback added this to the No action milestone May 2, 2018
@WillAyd WillAyd added the Window rolling, ewma, expanding label Sep 4, 2018
@qyqyq
Copy link

qyqyq commented Feb 6, 2021

Faced with the same problem, then what the implementation should be now? It seems that recent API of df.rolling() still has no para with respect to table, rather than a row or column.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

4 participants