# 7 examples of 'numpy rolling mean' in Python

Every line of 'numpy rolling mean' 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. ## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``81def mean(self):82    def mean(scol):83        return F.when(84            F.row_number().over(self._unbounded_window) &gt;= self._min_periods,85            F.mean(scol).over(self._window)86        ).otherwise(F.lit(None))8788    return self._apply_as_series_or_frame(mean)``
``273def rolling_mean_by_h(x, h, w, name):274    """Compute a rolling mean of x, after first aggregating by h.275276    Right-aligned. Computes a single mean for each unique value of h. Each277    mean is over at least w samples.278279    Parameters280    ----------281    x: Array.282    h: Array of horizon for each value in x.283    w: Integer window size (number of elements).284    name: Name for metric in result dataframe285286    Returns287    -------288    Dataframe with columns horizon and name, the rolling mean of x.289    """290    # Aggregate over h291    df = pd.DataFrame({'x': x, 'h': h})292    df2 = (293        df.groupby('h').agg(['mean', 'count']).reset_index().sort_values('h')294    )295    xm = df2['x']['mean'].values296    ns = df2['x']['count'].values297    hs = df2['h'].values298299    res_h = []300    res_x = []301    # Start from the right and work backwards302    i = len(hs) - 1303    while i &gt;= 0:304        # Construct a mean of at least w samples.305        n = int(ns[i])306        xbar = float(xm[i])307        j = i - 1308        while ((n &lt; w) and j &gt;= 0):309            # Include points from the previous horizon. All of them if still310            # less than w, otherwise just enough to get to w.311            n2 = min(w - n, ns[j])312            xbar = xbar * (n / (n + n2)) + xm[j] * (n2 / (n + n2))313            n += n2314            j -= 1315        if n &lt; w:316            # Ran out of horizons before enough points.317            break318        res_h.append(hs[i])319        res_x.append(xbar)320        i -= 1321    res_h.reverse()322    res_x.reverse()323    return pd.DataFrame({'horizon': res_h, name: res_x})``
``85def sma(data, span=100):86    """Computes and returns the simple moving average.8788    Note: the moving average is computed on all columns.8990    :Input:91     :data: pandas.DataFrame with stock prices in columns92     :span: int (defaul: 100), number of days/values over which93         the average is computed9495    :Output:96     :sma: pandas.DataFrame of simple moving average97    """98    return data.rolling(window=span, center=False).mean()``
``14def period_mean(data, freq):15    '''16        Method to calculate mean for each frequency17    '''18    return np.array(19        [np.mean(data[i::freq]) for i in range(freq)])``
``2155def hpat_pandas_series_mean_impl(self, axis=None, skipna=None, level=None, numeric_only=None):2156    if skipna is None:2157        skipna = True21582159    if skipna:2160        return numpy.nanmean(self._data)21612162    return self._data.mean()``
``314def __mean(xarr):315  """mean = x.sum() / len(x)""" #interface is [lb,ub]; not lb,ub316  from numpy import mean317  return mean(xarr)``
``174@rabbit175def mean(self):176    """Finds The Arithmetic Mean."""177    return sum(self.units)/float(len(self))``