Ibis Framework Documentation

repository·main·Indexed 27 days ago

https://github.com/ibis-project/ibis

Ibis is a portable Python dataframe library (version 12.0.0) that provides a common API for data manipulation across over 20 backends, including DuckDB, Polars, BigQuery, Snowflake, and SingleStoreDB. It allows developers to write Python code that compiles into optimized SQL or executes via high-performance dataframe engines, enabling seamless switching between local development and production databases.

Tokens
3.8K
Snippets
13
Records
23
Agent score
92%

What's inside Ibis

  1. Quickstart with Ibis and example data

    main

    After installation, you can enable interactive mode, fetch example data (like the penguins dataset), and perform data manipulations such as grouping and aggregating using the Ibis dataframe API.

    import ibis
    ibis.options.interactive = True
    t = ibis.examples.penguins.fetch()
    g = t.group_by("species", "island").agg(count=t.count()).order_by("count")
  2. Install Ibis with DuckDB and examples

    main

    You can install Ibis along with the DuckDB backend and example datasets using pip. This is a quick way to get started with local data exploration.

    pip install 'ibis-framework[duckdb,examples]'
  3. Switch between different Ibis backends

    main
    Ibis is portable. You can switch between different execution engines (like DuckDB, Polars, or DataFusion) by using ibis.set_backend() or by creating specific connection objects. This allows you to develop locally and deploy to remote production databases by changing only a single line of code.
  4. Mix SQL and Python code in Ibis

    main
    You can execute raw SQL queries directly on an Ibis connection and then continue using the Ibis Python API on the resulting object. This allows you to combine SQL's power with Python's flexibility.
  5. Create tables and temporary tables

    main

    You can create tables from an Ibis schema or directly from a Pandas DataFrame. Temporary tables created via create_table(..., temp=True) are automatically dropped when the connection closes.

    import ibis
    import pandas as pd
    
    # Create table from schema
    schema = ibis.schema([
        ('id', 'int64'),
        ('name', 'string'),
        ('price', 'float64'),
        ('created_at', 'timestamp')
    ])
    tbl = ibis_con.create_table('new_products', schema=schema)
    
    # Create temporary table from Pandas DataFrame
    temp_data = pd.DataFrame({"id": [1, 2, 3], "value": [10, 20, 30]})
    temp_table = ibis_con.create_table("temp_analysis", temp_data, temp=True)
  6. Create an Ibis client from an existing SingleStoreDB connection

    main

    If you already have an active connection using the singlestoredb Python SDK, you can wrap it in an Ibis client using ibis.singlestoredb.from_connection().

    import singlestoredb as s2
    import ibis
    
    # Create connection using SingleStoreDB client directly
    con = s2.connect(
        host="localhost",
        user="root",
        password="password",
        database="my_database"
    )
    
    # Create Ibis client from existing connection
    ibis_con = ibis.singlestoredb.from_connection(con)
  7. Connect to SingleStoreDB using a connection string

    main

    Use ibis.connect() with a URI-style connection string. For passwords containing special characters, ensure you use URL encoding (e.g., via urllib.parse.quote_plus).

    import ibis
    from urllib.parse import quote_plus
    
    # Basic connection string
    con = ibis.connect("singlestoredb://user:password@host:port/database")
    
    # With additional parameters
    con = ibis.connect("singlestoredb://user:password@host:port/database?autocommit=true&local_infile=1")
    
    # URL with special characters
    password = "p@ssw0rd!"
    encoded_password = quote_plus(password)
    con = ibis.connect(f"singlestoredb://user:{encoded_password}@host:port/database")
  8. Execute raw SQL in SingleStoreDB backend

    main

    The SingleStoreDB backend allows executing raw SQL queries. You can use raw_sql() which returns a cursor, or use it as a context manager for automatic cursor management.

    # Using context manager for automatic cursor management
    with ibis_con.raw_sql("SELECT COUNT(*) FROM users") as cursor:
        count = cursor.fetchone()[0]
    
    # Manual cursor management
    cursor = ibis_con.raw_sql("SHOW TABLES")
    tables = [row[0] for row in cursor.fetchall()]
    cursor.close()