A moving average smooths noisy data by replacing each point with the average of nearby values. In Python, the best method depends on your data shape: use Pandas for time series, NumPy for numeric arrays, and a small helper function for plain lists.
This guide covers simple moving averages, centered windows, missing values, NumPy convolution, and plotting the original series against the smoothed result.
Quick Method Guide
| Data | Recommended method | Why |
|---|---|---|
| Pandas Series or DataFrame | rolling().mean() |
Handles indexes, windows, and missing values cleanly |
| NumPy array | np.convolve() |
Fast vectorized smoothing for one-dimensional arrays |
| Small Python list | Manual window loop | No external dependency needed |
| Charting | Matplotlib line plot | Compare raw and smoothed values visually |
Moving Average with Pandas rolling()
For time series or tabular data, Pandas is usually the clearest option. The official pandas.Series.rolling() documentation covers fixed-size windows, minimum periods, centered windows, and time-based windows.
import pandas as pd
sales = pd.Series([10, 14, 13, 18, 21, 19, 25])
smoothed = sales.rolling(window=3).mean()
print(smoothed)
The first two rows are missing because a window of three values is not available yet. That is usually correct for analytics because it avoids pretending there is enough data before the window fills.
Use min_periods for Early Rows
If you want values before the full window is available, pass min_periods. This makes the first rows easier to plot, but remember that early values are based on fewer observations.
import pandas as pd
sales = pd.Series([10, 14, 13, 18, 21, 19, 25])
smoothed = sales.rolling(window=3, min_periods=1).mean()
print(smoothed)
When your data has dates, keep the date index attached. That makes the output easier to join, plot, and compare with calendar-based features. For market-calendar workflows, see PythonPool’s guide to pandas_market_calendars.
Centered Moving Average
By default, Pandas labels each moving average at the right edge of the window. Use center=True when you want the smoothed value aligned with the middle of the window instead.
import pandas as pd
values = pd.Series([4, 7, 9, 6, 11, 15, 14])
centered = values.rolling(window=3, center=True).mean()
print(centered)
A centered average is useful for visualization, but it can be wrong for forecasting because it uses future values. For a model that predicts tomorrow, use only past and current observations.
Moving Average with NumPy convolve()
For a one-dimensional numeric array, numpy.convolve() can compute a simple moving average by convolving the data with equal weights. NumPy’s official convolve() reference explains the available modes.
import numpy as np
values = np.array([10, 14, 13, 18, 21, 19, 25])
window = 3
weights = np.ones(window) / window
moving_average = np.convolve(values, weights, mode="valid")
print(moving_average)
The valid mode returns only positions where the full window overlaps the data. If you need output with the same length as the input, study the boundary behavior carefully before using same.
Moving Average for a Plain List
For small scripts, a plain Python function is fine. Use statistics.fmean() for a fast floating-point mean from the standard library. Python documents it in the statistics.fmean() reference.
from statistics import fmean
def moving_average(values, window):
if window <= 0:
raise ValueError("window must be positive")
return [
fmean(values[index:index + window])
for index in range(len(values) - window + 1)
]
print(moving_average([10, 14, 13, 18, 21, 19, 25], 3))
This function is easy to test and does not need Pandas or NumPy. Use it when the dataset is small and you do not need indexes, missing-value handling, or vectorized performance.
Plot Raw Data and Moving Average
A moving average is easier to judge visually. Plot the original data and smoothed data together. Matplotlib's official pyplot.plot() reference documents the line-plot API.
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.Series([10, 14, 13, 18, 21, 19, 25])
smoothed = sales.rolling(window=3, min_periods=1).mean()
plt.plot(sales.index, sales, label="Raw")
plt.plot(smoothed.index, smoothed, label="3-point moving average")
plt.legend()
plt.show()
If you also need uncertainty bands, compare this with PythonPool's Matplotlib errorbar guide. For model evaluation after smoothing, the mean squared error in Python tutorial is a useful next step.
Handling Missing Values
Pandas rolling windows skip missing values when possible, but the output still depends on min_periods. Decide whether missing values mean unknown data, zeros, or values that should be interpolated before smoothing.
import pandas as pd
values = pd.Series([10, None, 13, 18, None, 19, 25])
smoothed = values.rolling(window=3, min_periods=2).mean()
print(smoothed)
Bad missing-value masks can break data workflows before smoothing. PythonPool's guide to non-boolean masks with NA/NaN values explains one common Pandas error.
Common Mistakes
- Using a centered window for forecasting and accidentally including future data.
- Comparing a shorter NumPy
validresult with the original array without aligning indexes. - Choosing a window that is too large, which hides real changes in the data.
- Filling missing values with zero when zero is not a meaningful measurement.
- Forgetting that a DataFrame workflow may need column selection before rolling calculations.
For related DataFrame cleanup, see PythonPool's article on the deprecated DataFrame append attribute error. For NumPy transformations beyond smoothing, read the guides on numpy.piecewise() and numpy.amin(). For date labels in time series charts, see strftime in Python.
FAQs
What is the best moving average method in Python?
Use Pandas rolling().mean() for time series or DataFrames, NumPy convolve() for numeric arrays, and a small list helper for dependency-free scripts.
What does window size mean?
The window size is the number of observations used for each average. A larger window smooths more noise but reacts more slowly to changes.
Should I use centered moving averages?
Use centered moving averages for visualization and historical smoothing. Avoid them for forecasting because they use future observations.