-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Closed
Labels
BugNumeric OperationsArithmetic, Comparison, and Logical operationsArithmetic, Comparison, and Logical operationsWindowrolling, ewma, expandingrolling, ewma, expanding
Description
Seems like there is a bug in the centered mooving window functions, although the examples here don't show it.
Also np.convolve
used like in this post on Stackoverflow shows different results for the first values:
In [109]: ser = pd.Series(randn(20), index=pd.date_range('1/1/2000', periods=20))
In [110]: df = pd.DataFrame(ser)
In [111]: df['rc'] = pd.rolling_count(ser, 10, center=True)
In [112]: df['rm'] = pd.rolling_mean(ser, 10, center=True)
In [113]: df['rm_min_1'] = pd.rolling_mean(ser, 10, center=True, min_periods=1)
In [114]: def movingaverage(interval, window_size):
.....: window = numpy.ones(int(window_size))/float(window_size)
.....: return numpy.convolve(interval, window, 'same')
.....:
In [116]: df['ma'] = movingaverage(ser, 10)
In [117]: df
Out[117]:
0 rc rm rm_min_1 ma
2000-01-01 1.177692 5 NaN 0.048306 0.024153
2000-01-02 -0.068567 6 NaN -0.094287 -0.056572
2000-01-03 0.194942 7 NaN -0.225103 -0.157572
2000-01-04 -0.179778 8 NaN -0.160040 -0.128032
2000-01-05 -0.882760 9 NaN -0.262176 -0.235959
2000-01-06 -0.807252 10 -0.250828 -0.250828 -0.250828
2000-01-07 -1.009999 10 -0.365703 -0.365703 -0.365703
2000-01-08 0.295402 10 -0.431110 -0.431110 -0.431110
2000-01-09 -1.079267 10 -0.408968 -0.408968 -0.408968
2000-01-10 -0.148689 10 -0.456946 -0.456946 -0.456946
2000-01-11 0.028935 10 -0.290453 -0.290453 -0.290453
2000-01-12 -0.722632 10 -0.204166 -0.204166 -0.204166
2000-01-13 0.416364 10 -0.175837 -0.175837 -0.175837
2000-01-14 -0.659560 10 -0.266773 -0.266773 -0.266773
2000-01-15 0.782165 10 -0.184003 -0.184003 -0.184003
2000-01-16 0.055617 10 -0.338359 -0.338359 -0.338359
2000-01-17 -0.726706 0 NaN NaN -0.341252
2000-01-18 -0.613954 0 NaN NaN -0.268989
2000-01-19 -0.251574 0 NaN NaN -0.310625
2000-01-20 -1.692243 0 NaN NaN -0.244669
In [5]: import numpy
In [6]: In [20]: ser = pd.Series(range(10))
...: ...: df = pd.DataFrame(ser)
...: ...: N=3
...: ...: df['rc'] = ser.rolling(N, center=True).count()
...: ...: df['rm'] = ser.rolling(N, center=True).mean()
...: ...: df['rm_min_1'] = ser.rolling(N, center=True, min_periods=1).mean()
...: ...: def movingaverage(interval, window_size):
...: ...: window = numpy.ones(int(window_size))/float(window_size)
...: ...: return numpy.convolve(interval, window, 'same')
...: ...:
...: ...: df['ma'] = movingaverage(ser, N)
In [7]: df
Out[7]:
0 rc rm rm_min_1 ma
0 0 2.0 NaN 0.5 0.333333
1 1 3.0 1.0 1.0 1.000000
2 2 3.0 2.0 2.0 2.000000
3 3 3.0 3.0 3.0 3.000000
4 4 3.0 4.0 4.0 4.000000
5 5 3.0 5.0 5.0 5.000000
6 6 3.0 6.0 6.0 6.000000
7 7 3.0 7.0 7.0 7.000000
8 8 3.0 8.0 8.0 8.000000
9 9 2.0 NaN 8.5 5.666667
Metadata
Metadata
Assignees
Labels
BugNumeric OperationsArithmetic, Comparison, and Logical operationsArithmetic, Comparison, and Logical operationsWindowrolling, ewma, expandingrolling, ewma, expanding