import pandas as pd
df = pd.DataFrame({
'Sales': [100, 150, 200, 300]
})
print(df.expanding().mean())
Expected Output:
Sales
0 100.0
1 125.0
2 150.0
3 187.5
The DataFrame.expanding() method provides expanding window calculations, which are cumulative statistics where the window size grows with each subsequent row, starting from the beginning of the DataFrame. While standard rolling() windows maintain a fixed size (e.g., trailing 30 days), expanding() accumulates all historical records up to the current row. This makes it ideal for calculating Year-To-Date (YTD) totals, running lifetime averages, and structural maximum drawdowns over time.
DataFrame.expanding(min_periods=1, axis=0, method='single')min_periods: Integer (default 1). Minimum number of observations in window required to have a value; otherwise, result is NaN.axis: 0 or 'index' (apply down rows; default), 1 or 'columns' (apply across columns).method: 'single' or 'table' (default 'single'). Computes calculations per single column or across table rows..mean(), .sum(), .max(), .min(), or .std().
df['Price'].expanding().max() to track structural peak prices over time for financial risk modeling.Monitor lifetime peak values across time series datasets.
import pandas as pd
df_stock = pd.DataFrame({
'Price': [100, 105, 102, 110, 108]
})
df_stock['All_Time_High'] = df_stock['Price'].expanding().max()
print(df_stock)
Expected Output:
Price All_Time_High
0 100 100.0
1 105 105.0
2 102 105.0
3 110 110.0
4 108 110.0
Ensure statistical reliability by enforcing a minimum observation threshold before outputting calculations.
import pandas as pd
df_data = pd.DataFrame({'Val': [10, 20, 30, 40]})
# Require at least 3 periods before computing mean
print(df_data.expanding(min_periods=3).mean())
Q: What is the difference between expanding() and rolling()?
rolling() uses a fixed window size (e.g., last 5 rows). expanding() uses a growing window that starts from row 0 and includes all historical rows up to the current index.
Explore interactive code cells, test expanding window statistics, and execute self-validating assertions.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.