Track record highs over time
Time-ordered measurements
Pandas · Module
Returns the cumulative maximum along a DataFrame or Series axis. By default, it scans each column downward while preserving the input shape.
For each column by default, cummax scans rows from top to bottom and replaces each value with the largest value encountered so far. Set axis=1 to scan across columns within each row; skipna controls missing-value handling, while numeric_only restricts the operation to numeric and boolean columns.
DataFrame.cummax(axis=0, skipna=True, numeric_only=False, **args, **kwargs)| Name | Type | Description |
|---|---|---|
axis | {0 or 'index', 1 or 'columns'}, default 0 | Axis over which to calculate the cumulative maximum: 0 or 'index' scans rows, while 1 or 'columns' scans columns. Default: 0. |
skipna | bool, default True | Whether to exclude NA/null values. Default: True. |
numeric_only | bool, default False | Whether to include only float, integer, and boolean columns. Default: False. |
args | tuple | Additional arguments accepted for compatibility. |
kwargs | dict | Additional keyword arguments accepted for compatibility. |
DataFrame or Series of the same size containing cumulative maximum values.
import pandas as pd
prices = pd.DataFrame({"open": [10, 12, 11], "close": [11, 13, 12]})
prices["record_close"] = prices["close"].cummax()
print(prices)Tracks the highest closing price reached at each row.
Time-ordered measurements
Wide tabular records
Mixed-type DataFrames
DataFrame.cummax returns a DataFrame of the same size containing cumulative maxima. Its default axis=0 scans down each column, so every position reflects the greatest value up to that row.
Follow-up: How would you compute running maxima across each row? Use axis=1, which iterates over columns and computes the maximum seen from left to right.
skipna defaults to True, so missing values are excluded when calculating later cumulative maxima. However, an entirely missing row or column produces missing output.
Follow-up: What changes with skipna=False? Missing values participate in propagation, so a missing value can cause subsequent cumulative results along that axis to remain missing.
numeric_only defaults to False and determines whether all supported columns or only numeric and boolean columns are included. Setting it to True excludes non-numeric columns from the operation.
Follow-up: When is numeric_only=True useful? It is useful when a DataFrame mixes measurements with labels and the cumulative operation should apply only to measurement columns.
DataFrame.cumminDataFrame.cumprodDataFrame.cumsumSeries.cummax