Polars pandas modern dataframes

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Polars pandas modern dataframes is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. pandas: https://pandas.pydata.org/docs/

How to use this skill

Skill file

<!--

Polars: https://pola.rs/

pandas: https://pandas.pydata.org/docs/

Ibis: https://ibis-project.org/

Companion: role.md → "Capability reference → Dataframe libraries"

-->

Polars + pandas 2.x + Ibis — modern Python dataframes

Use Polars for >1GB / multi-core dataframes (Rust-backed, lazy, deterministic), pandas 2.x with PyArrow backend for legacy compatibility, and Ibis when you want to write code once and execute against any backend (Polars / pandas / DuckDB / Snowflake / BigQuery / Spark).

When to use

Defer SQL-only workflows to warehouse skills. Defer distributed (TB-scale) to Spark / Ray.

Setup

pip install polars                      # ~30MB; includes Arrow + parquet
pip install pandas pyarrow              # pandas 2.x with PyArrow backend
pip install 'ibis-framework[duckdb,snowflake,bigquery,polars]'   # backends as needed

No auth for local; backends inherit warehouse credentials.

When to pick which

| Need | Polars | pandas | Ibis |

|---|---|---|---|

| <100MB dataset | OK | best ergonomics | overkill |

| >1GB dataset | best | OOM risk | depends on backend |

| Lazy / query optimization | yes | no | yes (compiles to SQL) |

| Multi-core | yes (auto) | no (single core) | depends on backend |

| Streaming larger-than-RAM | yes | no | backend-dependent |

| Run on warehouse | no (export first) | no | yes (native) |

| Sklearn / ML library compatibility | via .to_numpy() | native | via Polars / pandas backend |

| Familiar API | needs learning | most familiar | new |

Common recipes — Polars

Recipe 1 — Read, transform, write

import polars as pl

# Eager
df = pl.read_parquet("orders.parquet")
df.filter(pl.col("amount") > 100).group_by("customer_id").agg(
    pl.col("amount").sum().alias("revenue"),
    pl.col("order_id").count().alias("orders"),
).sort("revenue", descending=True).write_parquet("customer_summary.parquet")

# Lazy (recommended for >100MB) — query plan optimized + executed in one shot
result = (
    pl.scan_parquet("data/orders/*.parquet")
      .filter(pl.col("status") == "paid")
      .group_by([pl.col("customer_id"), pl.col("order_date").dt.month_start().alias("month")])
      .agg(pl.col("amount").sum().alias("revenue"))
      .sort(["customer_id", "month"])
      .collect(streaming=True)        # streaming engine for larger-than-RAM
)

Recipe 2 — Joins + window functions

import polars as pl

orders = pl.scan_parquet("orders.parquet")
customers = pl.scan_parquet("customers.parquet")

result = (
    orders.join(customers, on="customer_id", how="left")
          .with_columns(
              # Running total per customer
              pl.col("amount")
                .cum_sum()
                .over("customer_id", order_by="order_date")
                .alias("lifetime_revenue"),
              # Rank within day
              pl.col("amount")
                .rank(method="dense", descending=True)
                .over("order_date")
                .alias("daily_rank"),
          )
          .collect()
)

Recipe 3 — Polars expression API (composable)

# Expressions are first-class — compose without re-querying
revenue_expr = pl.col("amount") * pl.col("quantity")
margin_expr = (revenue_expr - pl.col("cost")) / revenue_expr

df.with_columns(
    revenue=revenue_expr,
    margin=margin_expr,
    high_margin=margin_expr > 0.5,
)

# Conditional logic (when/then/otherwise)
df.with_columns(
    segment=pl.when(pl.col("revenue") > 10_000).then(pl.lit("whale"))
              .when(pl.col("revenue") > 1_000).then(pl.lit("regular"))
              .otherwise(pl.lit("casual"))
)

Recipe 4 — Pivots + unpivots

# Wide pivot
wide = df.pivot(index="customer_id", on="month", values="revenue", aggregate_function="sum")

# Long unpivot
long = wide.unpivot(index="customer_id", variable_name="month", value_name="revenue")

Recipe 5 — Date handling

df.with_columns(
    pl.col("order_date").dt.year().alias("year"),
    pl.col("order_date").dt.month_start().alias("month"),
    pl.col("order_date").dt.weekday().alias("dow"),
    days_since_signup=(pl.col("order_date") - pl.col("signup_date")).dt.total_days(),
)

# Date range
pl.date_range(date(2025, 1, 1), date(2025, 12, 31), interval="1d", eager=True)

Common recipes — pandas 2.x

Recipe 6 — PyArrow backend + Copy-on-Write

import pandas as pd

# Set globally
pd.options.mode.copy_on_write = True            # opt-in CoW — eliminates SettingWithCopyWarning

# Read with PyArrow backend (faster, lower memory)
df = pd.read_parquet("orders.parquet", dtype_backend="pyarrow")
df = pd.read_csv("orders.csv", engine="pyarrow", dtype_backend="pyarrow")

# Or per-DataFrame
df = pd.DataFrame({"x": [1, 2, 3]}, dtype="int64[pyarrow]")

# Nullable types (no NaN-coercion of integers)
df = pd.read_csv("orders.csv", dtype_backend="numpy_nullable")

Recipe 7 — pandas → Polars + back

import pandas as pd
import polars as pl

# pandas → polars (zero-copy via Arrow when possible)
pdf = pd.read_csv("data.csv")
pldf = pl.from_pandas(pdf)

# polars → pandas
back = pldf.to_pandas(use_pyarrow_extension_array=True)

Recipe 8 — DuckDB ↔ pandas/Polars zero-copy

import duckdb

# Query a DataFrame directly
df_pandas = pd.read_csv("data.csv")
duckdb.query("SELECT region, sum(amount) FROM df_pandas GROUP BY 1").pl()

df_polars = pl.read_parquet("data.parquet")
duckdb.query("SELECT region, sum(amount) FROM df_polars GROUP BY 1").pl()

Common recipes — Ibis

Recipe 9 — Write once, run on any backend

import ibis

# Backend = DuckDB local
con = ibis.duckdb.connect("analytics.duckdb")

# Backend = Snowflake (same code below)
# con = ibis.snowflake.connect(account=..., user=..., password=...)

orders = con.table("orders")
customers = con.table("customers")

result = (
    orders.left_join(customers, "customer_id")
          .filter(orders.amount > 100)
          .group_by([orders.customer_id, orders.order_date.month()])
          .aggregate(
              revenue=orders.amount.sum(),
              orders=orders.order_id.count(),
          )
          .order_by(ibis.desc("revenue"))
          .limit(100)
)

# Materialize (compiles to backend SQL, executes, returns DataFrame)
df = result.execute()              # → pandas DataFrame
df = result.to_polars()            # → Polars DataFrame
print(ibis.to_sql(result))         # inspect generated SQL per backend

Recipe 10 — Ibis + dbt for re-usable analytics

# Models defined in Python (reusable across DuckDB + Snowflake)
def customer_cohort_retention(con):
    orders = con.table("orders")
    cohorts = orders.group_by("customer_id").aggregate(
        cohort_month=orders.order_date.truncate("M").min(),
    )
    return (
        orders.left_join(cohorts, "customer_id")
              .group_by([cohorts.cohort_month,
                         (orders.order_date - cohorts.cohort_month).months().name("months_since")])
              .aggregate(active_users=orders.customer_id.nunique())
    )

# Execute on DuckDB for prototyping
local = ibis.duckdb.connect("analytics.duckdb")
df_prototype = customer_cohort_retention(local).execute()

# Same code on Snowflake for production
prod = ibis.snowflake.connect(...)
df_prod = customer_cohort_retention(prod).execute()

Example end-to-end

Goal: Compute monthly cohort retention from 3GB of order Parquet files; export to CSV for stakeholders.

  1. Use Polars lazy: lf = pl.scan_parquet("orders/*.parquet").
  2. Extract cohort month: cohort = lf.group_by("customer_id").agg(pl.col("order_date").min().alias("cohort")).
  3. Join back and compute months-since-cohort:

```python

retention = (

lf.join(cohort, on="customer_id")

.with_columns(

cohort_month=pl.col("cohort").dt.month_start(),

months_since=((pl.col("order_date").dt.year() - pl.col("cohort").dt.year()) * 12

)

.group_by(["cohort_month", "months_since"])

.agg(pl.col("customer_id").n_unique().alias("active_users"))

.sort(["cohort_month", "months_since"])

)

```

  1. wide = retention.collect(streaming=True).pivot(index="cohort_month", on="months_since", values="active_users").
  2. wide.write_csv("cohort_retention.csv").

Total: ~10s on a laptop for 3GB input.

Edge cases / gotchas

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.