API/ENH: master issue for pd.rolling_apply · Issue #8659 · pandas-dev/pandas (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

@leeong05

Description

@leeong05

Catchall for rolling_* issues:


Hi all,

I intended to apply a function that gives on each day a ranking based on means calculated from previous n-day's data. The natural way is to use pd.rolling_apply. A toy example:

In [93]: df = pd.DataFrame(np.random.randint(10, size=20).reshape(4, 5))

In [94]: df
Out[94]: 
   0  1  2  3  4
0  2  0  0  2  0
1  9  5  5  6  1
2  2  3  6  8  8
3  5  1  2  9  0

In [95]: import bottleneck as bn

In [96]: bn.nanrankdata(df.mean())
Out[96]: array([ 4. ,  1.5,  3. ,  5. ,  1.5])

Up to now, it is cool. Then:

In [97]: pd.rolling_apply(df, 2, lambda x: bn.nanrankdata(bn.nanmean(x, axis=0)))
Out[97]: 
    0   1   2   3   4
0 NaN NaN NaN NaN NaN
1   1   1   1   1   1
2   1   1   1   1   1
3   1   1   1   1   1

This is clearly wrong. Is this a bug?