import pytimetk as tk
import polars as pl
= tk.load_dataset("stocks_daily", parse_dates=["date"])
df
# Pandas DataFrame (engine inferred)
= (
dd_pd "symbol")
df.groupby(
.augment_drawdown(="date",
date_column="close",
close_column
)
)
# Polars DataFrame using the tk accessor
= (
dd_pl "symbol == 'AAPL'"))
pl.from_pandas(df.query(
.tk.augment_drawdown(="date",
date_column="close",
close_column
) )
augment_drawdown
augment_drawdown(
data,
date_column,
close_column,=False,
reduce_memory='auto',
engine )
Calculate running drawdown statistics for pandas or polars data.
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | DataFrame or GroupBy(pandas or polars) | Input time-series data. Grouped inputs are processed per group before the drawdown metrics are appended. | required |
date_column | str | Name of the column containing date information. | required |
close_column | str | Name of the column containing the values used to compute drawdowns. | required |
reduce_memory | bool | Attempt to reduce memory usage when operating on pandas data. If a polars input is supplied a warning is emitted and no conversion occurs. | False |
engine | (auto, pandas, polars) | Execution engine. "auto" (default) infers the backend from the input data while allowing explicit overrides. |
"auto" |
Returns
Name | Type | Description |
---|---|---|
DataFrame | DataFrame with the following columns appended: - {close_column}_peak - {close_column}_drawdown - {close_column}_drawdown_pct The return type matches the input backend. |
Notes
Drawdown measures the peak-to-trough decline of a series. The running peak is computed with a cumulative maximum per group (if present) and the drawdown percentage is expressed relative to that peak. When the peak is zero the percentage drawdown is left as NaN
to avoid division by zero.