Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:TA Lib Ta lib python Wrapper Decorator

From Leeroopedia


Knowledge Sources
Domains Data_Preparation, Technical_Analysis
Last Updated 2026-02-09 22:00 GMT

Overview

Concrete tool for transparent pandas/polars Series support provided by the _wrapper decorator function in talib/__init__.py.

Description

The _wrapper function is a decorator that wraps all 161 indicator functions and all streaming functions. It is applied at module import time in a loop:

for func_name in __TA_FUNCTION_NAMES__:
    wrapped_func = _wrapper(getattr(func, func_name))
    globals()[func_name] = wrapped_func

The wrapper handles:

  • Detection of polars.Series and pandas.Series inputs
  • Conversion to float64 numpy arrays via .to_numpy().astype(float)
  • Preservation of pandas DataFrame index
  • Conversion of outputs back to the original Series type
  • Streaming function detection (scalar results bypass Series conversion)

Usage

This decorator is applied automatically. Users simply pass pandas or polars Series to any TA-Lib function and receive the corresponding Series type back.

Code Reference

Source Location

  • Repository: ta-lib-python
  • File: talib/__init__.py
  • Lines: L40-106 (_wrapper function definition), L122-133 (wrapper application loop)

Signature

def _wrapper(func: Callable) -> Callable:
    """
    Decorator that adds pandas/polars Series support to TA-Lib functions.

    If pandas or polars is available, wraps func so that:
    - Series inputs are converted to numpy float64 arrays
    - Numpy outputs are converted back to Series (preserving pandas index)
    - Scalar streaming results are returned as-is
    - Mixing pandas and polars raises an Exception

    Args:
        func: The original TA-Lib C function binding

    Returns:
        Wrapped function with transparent type conversion
    """

Import

# _wrapper is internal, not imported directly.
# It is applied automatically to all functions.
import talib

I/O Contract

Inputs

Name Type Required Description
func Callable Yes The original TA-Lib Cython function to wrap

Outputs

Name Type Description
wrapper Callable Wrapped function supporting pandas/polars Series input/output

Usage Examples

Pandas Integration

import pandas as pd
import talib

# Create DataFrame with OHLCV data
df = pd.DataFrame({
    'open': [44.0, 44.3, 44.1, 43.6, 44.3],
    'high': [44.2, 44.5, 44.4, 44.1, 44.6],
    'low': [43.9, 44.0, 43.8, 43.4, 44.0],
    'close': [44.1, 44.3, 43.9, 44.0, 44.5],
    'volume': [100, 200, 150, 180, 120],
}, index=pd.date_range('2024-01-01', periods=5))

# Pass Series directly - index is preserved
df['sma_3'] = talib.SMA(df['close'], timeperiod=3)

Polars Integration

import polars as pl
import talib

df = pl.DataFrame({
    'close': [44.1, 44.3, 43.9, 44.0, 44.5] * 10,
})

sma = talib.SMA(df['close'], timeperiod=5)
# Returns polars.Series

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment