Python Pandas rolling window apply multiple lambda functions

Viewed 2696

Is it possible to apply more than one function to a pandas rolling window in a tuple like fashion?

I'm currently creating returning the count, lower bound and upper bound of the largest bin by count of a price series over a rolling window.

def qcut_data(x):
    data_out = pd.qcut(x, q = 10, duplicates = 'drop').value_counts().nlargest(1)
    return data_out


df_m['hist_count'] = df_m['mid'].rolling(window = rolling_window).apply(lambda x: qcut_data(x).item())
df_m['hist_bottom'] = df_m['mid'].rolling(window = rolling_window).apply(lambda x: qcut_data(x).index[0].left)
df_m['hist_top'] = df_m['mid'].rolling(window = rolling_window).apply(lambda x: qcut_data(x).index[0].right)

This strikes me as horribly inefficient as i'm just returning different values (item and index) of the same function (qcut_data), over the same rolling window of the same Series (df_m['mid']).

Is there a way to combine these so all three values can be returned?

I can't call a list, tuple or set in the .apply() method, is there another way?

1 Answers
Related