Every line of 'pandas moving average of column' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
236 def mean(self): 237 # TODO, there is a lot of copy-paste with the code above 238 # TODO, we should probably define groupby.aggregate 239 func = _accumulate_groupby_mean 240 start = (0, 0) 241 if isinstance(self.grouper, Streaming): 242 func = partial(func, index=self.index) 243 example = self.root.example.groupby(self.grouper.example) 244 if self.index is not None: 245 example = example[self.index] 246 example = example.mean() 247 stream = self.root.stream.zip(self.grouper.stream) 248 stream = stream.accumulate(func, start=start, returns_state=True) 249 else: 250 func = partial(func, grouper=self.grouper, index=self.index) 251 example = self.root.example.groupby(self.grouper) 252 if self.index is not None: 253 example = example[self.index] 254 example = example.mean() 255 stream = self.root.stream.accumulate(func, start=start, 256 returns_state=True) 257 if isinstance(example, pd.DataFrame): 258 return StreamingDataFrame(stream, example) 259 else: 260 return StreamingSeries(stream, example)
59 def average(average_window, data): 60 window = [] 61 newdata = [] 62 for v in data: 63 window.append(v) 64 if len(window) == average_window: 65 newdata.append(sum(window)/average_window) 66 del window[0] 67 return newdata
205 def wma(df: pd.DataFrame, period: int = 30, 206 col: str = pd_utils.CLOSE_COL) -> pd.Series: 207 """ 208 Weighted Moving Average. 209 210 :param df: 211 :param period: 212 :param col: 213 :return: 214 """ 215 wma_ = [] 216 217 for chunk in _chunks(df, period, col): 218 try: 219 wma_.append(_chunked_wma(chunk, period)) 220 except AttributeError: 221 wma_.append(None) 222 223 wma_.reverse() 224 return pd.Series(wma_, index=df.index, name='wma')