Skip to content

Commit 6cad535

Browse files
committed
added generic rolling_apply function
1 parent 819fb5f commit 6cad535

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

RELEASE.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ pandas 0.4 Release Notes
77
What is it
88
==========
99

10-
**pandas** is a library of powerful labeled data structures, statistical tools,
11-
and general code for working with time series and cross-sectional data. It was
12-
designed with the practical needs of statistical modeling and large,
13-
inhomogeneous data sets in mind. It is particularly well suited for, among other
14-
things, financial data analysis applications.
10+
**pandas** is a library of powerful labeled-axis data structures, statistical
11+
tools, and general code for working with time series and cross-sectional
12+
data. It was designed with the practical needs of statistical modeling and
13+
large, inhomogeneous data sets in mind. It is particularly well suited for,
14+
among other things, financial data analysis applications.
1515

1616
===============
1717
Where to get it

pandas/stats/moments.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
__all__ = ['rolling_count', 'rolling_max', 'rolling_min',
1616
'rolling_sum', 'rolling_mean', 'rolling_std', 'rolling_cov',
1717
'rolling_corr', 'rolling_var', 'rolling_skew', 'rolling_kurt',
18-
'rolling_quantile', 'rolling_median', 'ewma', 'ewmvar', 'ewmstd',
19-
'ewmvol', 'ewmcorr', 'ewmcov']
18+
'rolling_quantile', 'rolling_median', 'rolling_apply',
19+
'ewma', 'ewmvar', 'ewmstd', 'ewmvol', 'ewmcorr', 'ewmcov']
2020

2121
def rolling_count(arg, window, time_rule=None):
2222
"""
@@ -350,3 +350,27 @@ def call_cython(arg, window, minp):
350350
return _tseries.roll_quantile(arg, window, minp, quantile)
351351
return _rolling_moment(arg, window, call_cython, min_periods,
352352
time_rule=time_rule)
353+
354+
def rolling_apply(arg, window, func, min_periods=None, time_rule=None):
355+
"""Generic moving function application
356+
357+
Parameters
358+
----------
359+
arg : Series, DataFrame
360+
window : Number of observations used for calculating statistic
361+
func : function
362+
Must produce a single value from an ndarray input
363+
min_periods : int
364+
Minimum number of observations in window required to have a value
365+
time_rule : {None, 'WEEKDAY', 'EOM', 'W@MON', ...}, default=None
366+
Name of time rule to conform to before computing statistic
367+
368+
Returns
369+
-------
370+
y : type of input argument
371+
"""
372+
def call_cython(arg, window, minp):
373+
minp = _use_window(minp, window)
374+
return _tseries.roll_generic(arg, window, minp, func)
375+
return _rolling_moment(arg, window, call_cython, min_periods,
376+
time_rule=time_rule)

pandas/stats/tests/test_moments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ def alt(x):
6969

7070
self._check_moment_func(f, alt)
7171

72+
def test_rolling_apply(self):
73+
def roll_mean(x, window, min_periods=None, time_rule=None):
74+
return moments.rolling_apply(x, window,
75+
lambda x: x[np.isfinite(x)].mean(),
76+
min_periods=min_periods,
77+
time_rule=time_rule)
78+
self._check_moment_func(roll_mean, np.mean)
79+
7280
def test_rolling_std(self):
7381
self._check_moment_func(moments.rolling_std,
7482
lambda x: np.std(x, ddof=1))

0 commit comments

Comments
 (0)