ConnectorX

0.4.5 · active · verified Sat Apr 11

ConnectorX is a high-performance Python library for loading data from databases into dataframes (Pandas, Polars, Apache Arrow). Written in Rust, it bypasses Python's GIL, offering significant speedups for data-intensive operations. The current version is 0.4.5, and it has an active development cycle with frequent minor releases.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `connectorx.read_sql` to fetch data from a database into a Pandas DataFrame. Remember to replace the placeholder connection string with your actual database credentials and ensure `pandas` is installed. For a runnable example, set the `CX_DB_CONNECTION_STRING` environment variable to a valid database connection string.

import connectorx as cx
import pandas as pd
import os

# Example PostgreSQL connection string
# Replace with your actual database connection string
DB_CONNECTION_STRING = os.environ.get('CX_DB_CONNECTION_STRING', 'postgresql://user:password@host:5432/database')

if DB_CONNECTION_STRING == 'postgresql://user:password@host:5432/database':
    print("Warning: Please set CX_DB_CONNECTION_STRING environment variable for a real database connection.")
    print("Using a dummy connection string, this example might not run without a database.")
    # For a runnable dummy, you might use SQLite in-memory, but ConnectorX doesn't directly support that
    # The primary use case is external DBs.
    exit(1) # Exit if not configured, as it won't connect without a real string

query = "SELECT id, name FROM my_table WHERE id < 10"

try:
    df = cx.read_sql(DB_CONNECTION_STRING, query, return_type="pandas")
    print("Successfully read data:")
    print(df.head())
except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure your database connection string and query are correct, and the database is accessible.")

view raw JSON →