stockstats
raw JSON → 0.6.8 verified Fri May 01 auth: no python
A Python library that provides a DataFrame subclass with inline stock statistics support, including common technical indicators (e.g., SMA, EMA, RSI, MACD, Bollinger Bands) and more advanced indicators like Supertrend, Ichimoku Cloud, KST, and PVO. The current version is 0.6.8, with active development on GitHub. Release cadence is irregular, roughly a few releases per year.
pip install stockstats Common errors
error AttributeError: module 'stockstats' has no attribute 'StockDataFrame' ↓
cause Incorrect import: trying to use stockstats.StockDataFrame instead of importing the class directly.
fix
Use: from stockstats import StockDataFrame
error KeyError: 'rsi' ↓
cause Column name changed: in older versions, 'rsi' was the column; in new versions, it's 'rsi_14' (or other period).
fix
Access 'rsi_14' instead of 'rsi' (adjust period as needed).
error ValueError: cannot reindex from a duplicate axis ↓
cause DataFrame index has duplicate timestamps (e.g., from intraday data with same day). The library does not handle duplicates gracefully.
fix
Ensure index is unique: df = df[~df.index.duplicated(keep='first')]
Warnings
breaking In version 0.6.x, the column naming convention for some indicators changed (e.g., 'rsi' became 'rsi_14'). Old code using 'rsi' without the period will break. ↓
fix Update column names to include the period (e.g., 'rsi_14', 'macd').
breaking The method _tp() previously returned a scalar when the result was constant; in 0.6.7 it was fixed to always return a pandas Series. Code that relied on scalar output (e.g., item access) may break. ↓
fix Ensure code handles Series output instead of scalar.
gotcha yfinance may change its API (e.g., 'period' parameter). StockDataFrame expects columns: 'open', 'high', 'low', 'close', 'volume'. If yfinance changes column names, the library may not recognize them. ↓
fix Use yfinance with keyword arguments for column mapping, or ensure column names match the expected format.
gotcha Many indicators can produce NaN values due to division by zero or insufficient data length. These are silently ignored in some versions, leading to misleading results. ↓
fix Upgrade to 0.6.6+ which fixes division-by-zero issues (PR #201).
Imports
- StockDataFrame wrong
import stockstats stockstats.StockDataFramecorrectfrom stockstats import StockDataFrame
Quickstart
import yfinance as yf
from stockstats import StockDataFrame
# Download historical data for AAPL
df = yf.download('AAPL', start='2023-01-01', end='2023-12-31')
# Convert to StockDataFrame
stock = StockDataFrame.retype(df)
# Access RSI (14-day) and MACD
print(stock['rsi_14'].tail())
print(stock['macd'].tail())