Polars

From mkurman/zorai · View interactive version

Polars is an AI agent skill from the GitHub repository mkurman/zorai. Fast in-memory DataFrame library for datasets that fit in RAM. Use when pandas is too slow but data still fits in memory. Lazy evaluation, parallel execution, Apache Arrow backend. Best for 1-100GB datasets, ETL pipelines, faster pandas replacement. For larger-than-RAM data use dask or vaex.

How to use this skill

Skill file

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

| Select column | df["col"] | df.select("col") |

| Filter | df[df["col"] > 10] | df.filter(pl.col("col") > 10) |

| Add column | df.assign(x=...) | df.with_columns(x=...) |

| Group by | df.groupby("col").agg(...) | df.group_by("col").agg(...) |

| Window | df.groupby("col").transform(...) | df.with_columns(...).over("col") |

Key Syntax Patterns

Pandas sequential (slow):

df.assign(
    col_a=lambda df_: df_.value * 10,
    col_b=lambda df_: df_.value * 100
)

Polars parallel (fast):

df.with_columns(
    col_a=pl.col("value") * 10,
    col_b=pl.col("value") * 100,
)

For comprehensive migration guide, load references/pandas_migration.md.

Best Practices

Performance Optimization

  1. Use lazy evaluation for large datasets:

```python

lf = pl.scan_csv("large.csv") # Don't use read_csv

result = lf.filter(...).select(...).collect()

```

  1. Avoid Python functions in hot paths:
  1. Use streaming for very large data:

```python

lf.collect(streaming=True)

```

  1. Select only needed columns early:

```python

# Good: Select columns early

lf.select("col1", "col2").filter(...)

# Bad: Filter on all columns first

lf.filter(...).select("col1", "col2")

```

  1. Use appropriate data types:

Expression Patterns

Conditional operations:

pl.when(condition).then(value).otherwise(other_value)

Column operations across multiple columns:

df.select(pl.col("^.*_value$") * 2)  # Regex pattern

Null handling:

pl.col("x").fill_null(0)
pl.col("x").is_null()
pl.col("x").drop_nulls()

For additional best practices and patterns, load references/best_practices.md.

Resources

This skill includes comprehensive reference documentation:

references/

Load these references as needed when users require detailed information about specific topics.

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.