utilities.trim()

Trim a numeric vector by setting extreme observations to NaN.

Usage

Source

utilities.trim(
    x,
    cut,
)

Sets values below the lower ‘cut’ quantile and above the upper ‘1 - cut’ quantile to NaN. The returned vector has the same length as the input, so it can be assigned back to a column.

Parameters

x: numpy.ndarray or polars.Series

A numeric vector to trim.

cut: float
Proportion of observations set to NaN at each tail. For example, ‘cut=0.05’ sets the lowest and highest five percent to NaN. Must lie in ‘[0, 0.5]’.

Returns

numpy.ndarray
Float vector with the same length as ‘x’ in which the extreme observations are NaN. Missing values in ‘x’ stay missing.

Examples

import numpy as np
from tidyfinance import trim
rng = np.random.default_rng(123)
data = rng.standard_normal(100)
trimmed = trim(data, 0.05)