augment_rsi

augment_rsi(
    data,
    date_column,
    close_column,
    periods=14,
    reduce_memory=False,
    engine='auto',
)

Calculate the Relative Strength Index (RSI) for pandas or polars data.

Parameters

Name Type Description Default
data DataFrame or GroupBy(pandas or polars) Financial data to augment. Grouped inputs are processed per group before RSI columns are appended. required
date_column str or ColumnSelector Name of the column containing date information. Accepts tidy selectors such as contains("date"). required
close_column str, ColumnSelector, or list Column name(s) containing the closing prices used to compute RSI. Lists (or multiple selectors) generate an RSI for each column. required
periods int, tuple, or list Lookback window(s) used when computing RSI. Accepts an integer, an inclusive tuple range, or a list of explicit periods. Defaults to 14. 14
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, cudf) Execution engine. "auto" (default) infers the backend from the input data while allowing explicit overrides. "auto"

Returns

Name Type Description
DataFrame DataFrame with {close_column}_rsi_{period} columns appended. The return type matches the input backend.

Notes

RSI follows Wilder’s formulation, separating gains and losses before computing smoothed averages and forming the ratio. Values range from 0 to 100, with extreme readings typically interpreted as overbought or oversold. Division-by-zero cases yield NaN which mirrors pandas behaviour.

Examples

import pandas as pd
import polars as pl
import pytimetk as tk


df = tk.load_dataset("stocks_daily", parse_dates=["date"])

# Pandas example (engine inferred)
rsi_pd = (
    df.groupby("symbol")
    .augment_rsi(
        date_column="date",
        close_column="close",
        periods=[14, 28],
    )
)

# Polars example using the tk accessor
rsi_pl = (
    pl.from_pandas(df.query("symbol == 'AAPL'"))
    .tk.augment_rsi(
        date_column="date",
        close_column=["close"],
        periods=14,
    )
)

# Selector example
from pytimetk.utils.selection import contains
selector_demo = (
    df
        .augment_rsi(
            date_column=contains("dat"),
            close_column=contains("clos"),
            periods=[14],
        )
)