@@ -66,9 +66,9 @@ def ols(trendline_options, x_raw, x, y, x_label, y_label, non_missing):
66
66
fit_results .params [0 ],
67
67
)
68
68
elif not add_constant :
69
- hover_header += "%s = %g * %s<br>" % (y_label , fit_results .params [0 ], x_label , )
69
+ hover_header += "%s = %g * %s<br>" % (y_label , fit_results .params [0 ], x_label )
70
70
else :
71
- hover_header += "%s = %g<br>" % (y_label , fit_results .params [0 ], )
71
+ hover_header += "%s = %g<br>" % (y_label , fit_results .params [0 ])
72
72
hover_header += "R<sup>2</sup>=%f<br><br>" % fit_results .rsquared
73
73
return y_out , hover_header , fit_results
74
74
@@ -91,27 +91,48 @@ def lowess(trendline_options, x_raw, x, y, x_label, y_label, non_missing):
91
91
return y_out , hover_header , None
92
92
93
93
94
- def ma (trendline_options , x_raw , x , y , x_label , y_label , non_missing ):
95
- """Moving Average (MA) trendline function
94
+ def _pandas (mode , trendline_options , x_raw , y , non_missing ):
95
+ modes = dict (rolling = "Rolling" , ewm = "Exponentially Weighted" , expanding = "Expanding" )
96
+ function_name = trendline_options .pop ("function" , "mean" )
97
+ function_args = trendline_options .pop ("function_args" , dict ())
98
+ series = pd .Series (y , index = x_raw )
99
+ agg = getattr (series , mode ) # e.g. series.rolling
100
+ agg_obj = agg (** trendline_options ) # e.g. series.rolling(**opts)
101
+ function = getattr (agg_obj , function_name ) # e.g. series.rolling(**opts).mean
102
+ y_out = function (** function_args ) # e.g. series.rolling(**opts).mean(**opts)
103
+ y_out = y_out [non_missing ]
104
+ hover_header = "<b>%s %s trendline</b><br><br>" % (modes [mode ], function_name )
105
+ return y_out , hover_header , None
106
+
96
107
97
- Requires `pandas` to be installed.
108
+ def rolling (trendline_options , x_raw , x , y , x_label , y_label , non_missing ):
109
+ """Rolling trendline function
98
110
99
- The `trendline_options` dict is passed as keyword arguments into the
100
- `pandas.Series.rolling` function.
111
+ The value of the `function` key of the `trendline_options` dict is the function to
112
+ use (defaults to `mean`) and the value of the `function_args` key are taken to be
113
+ its arguments as a dict. The remainder of the `trendline_options` dict is passed as
114
+ keyword arguments into the `pandas.Series.rolling` function.
101
115
"""
102
- y_out = pd .Series (y , index = x_raw ).rolling (** trendline_options ).mean ()[non_missing ]
103
- hover_header = "<b>MA trendline</b><br><br>"
104
- return y_out , hover_header , None
116
+ return _pandas ("rolling" , trendline_options , x_raw , y , non_missing )
105
117
106
118
107
- def ewma (trendline_options , x_raw , x , y , x_label , y_label , non_missing ):
108
- """Exponentially Weighted Moving Average (EWMA) trendline function
119
+ def expanding (trendline_options , x_raw , x , y , x_label , y_label , non_missing ):
120
+ """Expanding trendline function
109
121
110
- Requires `pandas` to be installed.
122
+ The value of the `function` key of the `trendline_options` dict is the function to
123
+ use (defaults to `mean`) and the value of the `function_args` key are taken to be
124
+ its arguments as a dict. The remainder of the `trendline_options` dict is passed as
125
+ keyword arguments into the `pandas.Series.expanding` function.
126
+ """
127
+ return _pandas ("expanding" , trendline_options , x_raw , y , non_missing )
111
128
112
- The `trendline_options` dict is passed as keyword arguments into the
113
- `pandas.Series.ewma` function.
129
+
130
+ def ewm (trendline_options , x_raw , x , y , x_label , y_label , non_missing ):
131
+ """Exponentially weighted trendline function
132
+
133
+ The value of the `function` key of the `trendline_options` dict is the function to
134
+ use (defaults to `mean`) and the value of the `function_args` key are taken to be
135
+ its arguments as a dict. The remainder of the `trendline_options` dict is passed as
136
+ keyword arguments into the `pandas.Series.ewm` function.
114
137
"""
115
- y_out = pd .Series (y , index = x_raw ).ewm (** trendline_options ).mean ()[non_missing ]
116
- hover_header = "<b>EWMA trendline</b><br><br>"
117
- return y_out , hover_header , None
138
+ return _pandas ("ewm" , trendline_options , x_raw , y , non_missing )
0 commit comments