Skip to content

DAAF-Contribution-Community/daaf / .claude/skills/polars

polars

Verified

High-performance data manipulation with lazy/eager execution, expressions, I/O (CSV, Parquet, JSON), aggregations, joins, string/datetime ops, and pandas interop. Covers performance optimization patterns and common anti-patterns. DAAF's default DataFrame library — all pipeline code uses Polars, not pandas. Use for any DataFrame operation, reading/writing Parquet files, or migrating existing pandas code to Polars. R equivalent: tidyverse (use when execution language is R).

references/io-data.md

View on GitHub
# Data I/O

> **Package availability:** Several readers below name optional backend packages
> via `# Requires: pip install X` comments. These are package-identity references,
> not instructions to run. In the DAAF container only **openpyxl** (Excel reads)
> is pre-installed; the other named backends — **xlsx2csv, connectorx, fsspec,
> s3fs, gcsfs, adlfs, and deltalake** — are **not** present, and runtime installs
> are blocked (`pip install`/`uv add` are refused both at the command line and
> inside executed scripts — see CLAUDE.md § Runtime Package Installation). Parquet,
> CSV, JSON/NDJSON, IPC/Arrow, and Avro all work with the base install. To use an
> absent backend (databases via connectorx/ADBC, cloud storage, or Delta Lake),
> escalate to add it to the Dockerfile user-additions block and rebuild.

## CSV Files

### Read CSV (Eager)

```python
df = pl.read_csv("data.csv")

# With options
df = pl.read_csv(
    "data.csv",
    separator=",",              # Delimiter (default: ",")
    has_header=True,            # First row is header (default: True)
    skip_rows=1,                # Skip first N rows
    n_rows=1000,                # Only read first N rows
    columns=["a", "b", "c"],    # Only these columns
    schema_overrides={"id": pl.Int32},  # Override types ("dtypes" renamed to "schema_overrides" in 0.20.31)
    null_values=["NA", ""],     # Treat as null
    ignore_errors=True,         # Skip bad rows
    encoding="utf8",            # Encoding (default: utf8)
)
```

### Scan CSV (Lazy - Recommended)

```python
lf = pl.scan_csv("data.csv")

# With options
lf = pl.scan_csv(
    "data.csv",
    has_header=True,
    separator=",",
    skip_rows=0,
    n_rows=None,                # None = all rows
    schema_overrides={"id": pl.Int32},
    null_values=["NA"],
    infer_schema_length=10000,  # Rows to infer types (default: 100)
    low_memory=False,           # Reduce memory at cost of speed
)

df = lf.collect()
```

### Write CSV

```python
df.write_csv("output.csv")

# With options
df.write_csv(
    "output.csv",
    separator=",",
    include_header=True,
    null_value="",              # How to write nulls
    datetime_format="%Y-%m-%d", # Format for datetime
    float_precision=6,          # Decimal places
)
```

### Streaming Write (Large Data)

```python
lf.sink_csv("output.csv")
```

## Parquet Files (Recommended)

Parquet is the preferred format: compressed, fast, type-preserving.

### Read Parquet (Eager)

```python
df = pl.read_parquet("data.parquet")

# Specific columns only
df = pl.read_parquet("data.parquet", columns=["a", "b"])

# Row groups
df = pl.read_parquet("data.parquet", n_rows=1000)
```

### Scan Parquet (Lazy - Best for Large Files)

```python
lf = pl.scan_parquet("data.parquet")

# Benefits: predicate/projection pushdown
result = (
    pl.scan_parquet("huge.parquet")
    .filter(pl.col("date") > pl.date(2024, 1, 1))  # Only reads matching rows
    .select("id", "value")                          # Only reads these columns
    .collect()
)
```

### Write Parquet

```python
df.write_parquet("output.parquet")

# With compression
df.write_parquet(
    "output.parquet",
    compression="zstd",         # "zstd", "snappy", "gzip", "lz4", "uncompressed"
    compression_level=3,        # 1-22 for zstd (higher = smaller, slower)
    statistics=True,            # Include column statistics
    row_group_size=100_000,     # Rows per group
)
```

### Streaming Write

```python
lf.sink_parquet("output.parquet", compression="zstd")
```

## JSON Files

### Read JSON

```python
# Standard JSON (array of objects)
df = pl.read_json("data.json")
```

### Read NDJSON (Newline-Delimited JSON)

```python
# Better for large files, streaming
df = pl.read_ndjson("data.ndjson")

# Lazy scan
lf = pl.scan_ndjson("data.ndjson")
```

### Write JSON/NDJSON

```python
df.write_json("output.json")
df.write_ndjson("output.ndjson")

# Streaming
lf.sink_ndjson("output.ndjson")
```

## Multiple Files (Glob Patterns)

### Read Multiple Files

```python
# All CSVs in directory
lf = pl.scan_csv("data/*.csv")

# Recursive glob
lf = pl.scan_parquet("data/**/*.parquet")

# With pattern matching
lf = pl.scan_csv("data/sales_202[34]*.csv")
```

### Add Source File Column

```python
lf = pl.scan_csv("data/*.csv", include_file_paths="source_file")
# Adds column with the file path for each row
```

### Hive Partitioned Data

```python
# For directory structure: data/year=2024/month=01/data.parquet
lf = pl.scan_parquet("data/**/*.parquet", hive_partitioning=True)
# Automatically creates year and month columns from path
```

## Excel Files

```python
# Requires: pip install xlsx2csv or pip install openpyxl

df = pl.read_excel("data.xlsx")

# Specific sheet
df = pl.read_excel("data.xlsx", sheet_name="Sheet2")
df = pl.read_excel("data.xlsx", sheet_id=1)  # 1-indexed

# Read all sheets
sheets = pl.read_excel("data.xlsx", sheet_id=0)  # Returns dict

# Write Excel
df.write_excel("output.xlsx")
```

## Database Connections

### SQLite

```python
# Read
df = pl.read_database(
    "SELECT * FROM users WHERE active = 1",
    "sqlite:///database.db"
)

# Or with connection object
import sqlite3
conn = sqlite3.connect("database.db")
df = pl.read_database("SELECT * FROM users", conn)
```

### PostgreSQL

```python
# Requires: pip install connectorx

df = pl.read_database(
    "SELECT * FROM users",
    "postgresql://user:pass@host:5432/database"
)

# With query parameters
df = pl.read_database_uri(
    "SELECT * FROM users WHERE id = $1",
    "postgresql://user:pass@host/db",
    execute_options={"parameters": [42]}
)
```

### MySQL

```python
df = pl.read_database(
    "SELECT * FROM users",
    "mysql://user:pass@host:3306/database"
)
```

### Generic ADBC (Arrow Database Connectivity)

```python
# Modern, high-performance option
import adbc_driver_postgresql.dbapi

conn = adbc_driver_postgresql.dbapi.connect("postgresql://...")
df = pl.read_database("SELECT * FROM users", conn)
```

## Cloud Storage

### AWS S3

```python
# Requires: pip install polars[fsspec] and s3fs

# Read directly
df = pl.read_parquet("s3://bucket/path/data.parquet")
lf = pl.scan_parquet("s3://bucket/path/*.parquet")

# With credentials (via environment or explicit)
import s3fs
fs = s3fs.S3FileSystem(
    key="ACCESS_KEY",
    secret="SECRET_KEY",
    endpoint_url="https://s3.region.amazonaws.com"
)
df = pl.read_parquet("s3://bucket/data.parquet", storage_options={"fs": fs})
```

### Google Cloud Storage

```python
# Requires: pip install gcsfs

df = pl.read_parquet("gs://bucket/data.parquet")
lf = pl.scan_parquet("gs://bucket/path/*.parquet")
```

### Azure Blob Storage

```python
# Requires: pip install adlfs

df = pl.read_parquet("az://container/data.parquet")
lf = pl.scan_parquet("abfss://container@account.dfs.core.windows.net/path/*.parquet")
```

## HTTP/URLs

```python
# Read directly from URL
df = pl.read_csv("https://example.com/data.csv")
df = pl.read_parquet("https://example.com/data.parquet")

# Large files - download first for better performance
import urllib.request
urllib.request.urlretrieve("https://example.com/large.parquet", "local.parquet")
df = pl.read_parquet("local.parquet")
```

## IPC (Arrow/Feather)

```python
# Fast binary format, good for inter-process communication

# Read
df = pl.read_ipc("data.arrow")
df = pl.read_ipc("data.feather")

# Lazy scan
lf = pl.scan_ipc("data.arrow")

# Write
df.write_ipc("output.arrow")
df.write_ipc("output.feather", compression="zstd")
```

## Avro Files

```python
# Avro support is built into polars (no extra install needed)

df = pl.read_avro("data.avro")
df.write_avro("output.avro", compression="snappy")
```

## Delta Lake

```python
# Requires: pip install deltalake

df = pl.read_delta("delta_table_path/")
lf = pl.scan_delta("delta_table_path/")

# With version
# Version is passed directly: pl.read_delta("path/", version=5)
df = pl.read_delta("delta_table/", version=5)
```

## Common Patterns

### Type Inference Control

```python
# More rows for type inference
lf = pl.scan_csv("data.csv", infer_schema_length=100000)

# Disable inference (use explicit schema)
df = pl.read_csv("data.csv", infer_schema=False)  # All strings

# Explicit schema
df = pl.read_csv(
    "data.csv",
    schema={
        "id": pl.Int64,
        "name": pl.String,
        "date": pl.Date,
        "value": pl.Float64
    }
)
```

### Handling Large Files

```python
# Use lazy + streaming
result = (
    pl.scan_csv("huge_file.csv")
    .filter(pl.col("category") == "A")
    .group_by("region")
    .agg(pl.col("sales").sum())
    .collect(engine="streaming")
)

# Or sink directly to file
(
    pl.scan_csv("input.csv")
    .filter(pl.col("active"))
    .sink_parquet("output.parquet")
)
```

### Reading Specific Rows/Columns

```python
# Only specific columns
df = pl.read_parquet("data.parquet", columns=["id", "name", "value"])

# Only first N rows
df = pl.read_csv("data.csv", n_rows=1000)

# Skip rows
df = pl.read_csv("data.csv", skip_rows=10, skip_rows_after_header=5)
```

## Summary

| Format | Read (Eager) | Scan (Lazy) | Write | Best For |
|--------|--------------|-------------|-------|----------|
| CSV | `read_csv` | `scan_csv` | `write_csv` | Interchange |
| Parquet | `read_parquet` | `scan_parquet` | `write_parquet` | Production (recommended) |
| JSON | `read_json` | - | `write_json` | APIs |
| NDJSON | `read_ndjson` | `scan_ndjson` | `write_ndjson` | Streaming JSON |
| IPC/Arrow | `read_ipc` | `scan_ipc` | `write_ipc` | Inter-process |
| Excel | `read_excel` | - | `write_excel` | Spreadsheets |
| Database | `read_database` | - | - | SQL queries |

**Key recommendation**: Use Parquet with lazy scanning (`scan_parquet`) for large datasets.
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 322kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall