Панды вращаются, применяют пользовательские функции, которые возвращают ndarrays - PullRequest
0 голосов
/ 31 марта 2019

Скажем, у меня есть DataFrame, индекс которого - время, и я хочу вычислить один ndarray для каждого временного окна по очереди.Как я могу сделать это, используя pandas rolling функциональность?

Например:

import pandas as pd
import numpy as np

np.random.seed(1)

df = pd.DataFrame(
    index=pd.period_range('2018-01-01', periods=100, freq='D'),
    columns=range(3),
    data=np.random.randn(100, 3)
)


def custom_func(sub_df: np.ndarray) -> np.ndarray:
    # do something, return a matrix
    # fora minimal example, return sub_df.T @ sub_df
    # the real task is much more complicated...
    return sub_df.T @ sub_df

# can I apply custom_func using rolling?
# the return should be a 3D numpy array
# like
# resarray = df.rolling(window=5).apply(custom_func)
...