Scriptone Scriptone

Published yfinance-pl to PyPI

blog-image

Overview

I published yfinance-pl to PyPI. It’s a stock data retrieval library that wraps yfinance-rs for Python with enhanced type safety. Unlike the traditional yfinance, it returns pl.DataFrame instead of pd.DataFrame.

Why I Created This

Recently, I’ve had opportunities to use Polars as an alternative to Pandas, and it’s gradually gaining adoption as modern libraries add Polars support. Polars is a high-performance DataFrame library built in Rust, offering benefits like faster speed and lower memory consumption compared to Pandas. For handling large-scale data analysis and Polars-only processing pipelines, I believed that having yfinance—the gateway to stock analysis—natively support Polars would enable smoother analysis and a better developer experience. I wanted users who prefer analyzing with Polars to do so without worrying about whether they’re dealing with Polars DataFrames or Pandas DataFrames. That’s why I wrapped yfinance-rs with enhanced type safety to implement yfinance-pl.

Features

yfinance-Compatible API

You can fetch stock data using the same API as yfinance. Existing code using yfinance can be migrated with minimal changes.

import yfinance_pl as yf

ticker = yf.Ticker("AAPL")
history = ticker.history(period="1mo")

Rust Backend

It wraps yfinance-rs with PyO3, so data fetching is executed in Rust.

Type-Safe API

The period and interval parameters use Literal types, enabling IDE autocompletion.

# period accepts: "1d", "5d", "1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"
# interval accepts: "1m", "2m", "5m", "15m", "30m", "60m", "90m", "1h", "1d", "5d", "1wk", "1mo", "3mo"
history = ticker.history(period="1mo", interval="1d")

Additionally, return values like TickerInfo, FastInfo, and CalendarInfo have TypedDict definitions, so dictionary keys are also autocompleted in your IDE.

Comparison with yfinance

Itemyfinanceyfinance-pl
DataFrame Typepd.DataFramepl.DataFrame
BackendPythonRust (yfinance-rs)
Series Typepd.Seriespl.DataFrame
Type HintsPartialSupported

Supported Features

yfinance-pl supports the following features:

CategoryProperty/MethodReturn Type
Price Historyhistory()pl.DataFrame
Company Infoinfo, fast_info, calendardict
Dividends & Splitsdividends, splits, actions, capital_gainspl.DataFrame
Financial Statementsincome_stmt, balance_sheet, cashflowpl.DataFrame
Quarterly Financialsquarterly_income_stmt, quarterly_balance_sheet, quarterly_cashflowpl.DataFrame
Shareholder Infomajor_holders, institutional_holders, mutualfund_holderspl.DataFrame
Insiderinsider_transactions, insider_roster_holderspl.DataFrame
Analystrecommendations, upgrades_downgradespl.DataFrame
Optionsoptions, option_chain()list[str], OptionChain

Usage Examples

Fetching Price History

import yfinance_pl as yf

ticker = yf.Ticker("AAPL")

# Daily data for the last month
history = ticker.history(period="1mo", interval="1d")
print(history)

Output example:

shape: (21, 7)
┌─────────────────────────┬────────────┬────────────┬────────────┬────────────┬──────────────┬───────────┐
│ date                    ┆ open       ┆ high       ┆ low        ┆ close      ┆ volume       ┆ dividends │
│ ---                     ┆ ---        ┆ ---        ┆ ---        ┆ ---        ┆ ---          ┆ ---       │
│ datetime[ns, UTC]       ┆ f64        ┆ f64        ┆ f64        ┆ f64        ┆ i64          ┆ f64       │
╞═════════════════════════╪════════════╪════════════╪════════════╪════════════╪══════════════╪═══════════╡
│ 2024-10-28 00:00:00 UTC ┆ 233.160004 ┆ 234.729996 ┆ 232.550003 ┆ 233.399994 ┆ 23819200     ┆ 0.0       │
│ ...                     ┆ ...        ┆ ...        ┆ ...        ┆ ...        ┆ ...          ┆ ...       │
└─────────────────────────┴────────────┴────────────┴────────────┴────────────┴──────────────┴───────────┘

Fetching Financial Statements

# Income Statement
income = ticker.income_stmt
print(income)

# Balance Sheet
balance = ticker.balance_sheet
print(balance)

# Cash Flow Statement
cashflow = ticker.cashflow
print(cashflow)

Fetching Option Chains

# Get available expiration dates
dates = ticker.options
print(dates)

# Get option chain for a specific expiration date
chain = ticker.option_chain("2024-12-20")
print(chain.calls)  # Call options
print(chain.puts)   # Put options

Installation

It’s uploaded to PyPI, so you can install it with pip. You can also use uv.

pip install yfinance-pl

Or:

uv add yfinance-pl

Future Updates

I plan to update yfinance-pl alongside updates to yfinance-rs. If there are improvements on the Python side, like the type safety enhancements, I’ll implement those as well.

comment

Loading comments...