Polars for Rust: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Polars Library for Rust: Complete Guide

Polars is a high-performance DataFrame library written in Rust. It offers a familiar pandas-like API while significantly outperforming it in speed and memory efficiency. Unlike pandas, which often hits bottlenecks with single-threaded execution and RAM consumption, Polars is designed from the ground up for multi-threaded operation and uses lazy evaluation (lazy API) to optimize queries.

The main goal of Polars is to provide a tool for processing large datasets—from hundreds of megabytes to several terabytes—on a single node, without resorting to distributed systems like Spark. This is achieved through efficient memory management (zero-copy, Apache Arrow backend), vectorized operations, and automatic parallelization. For Rust developers, Polars isn't just a wrapper around a Python library—it's a native library that fully leverages the language's strengths: memory safety, type system, and performance.

The library offers two APIs: Eager (immediate execution) and Lazy (lazy evaluation). The Eager API is great for interactive work and small datasets when you need instant results. The Lazy API lets you build a query plan that gets optimized and executed in a single pass—critical for complex transformations and big data. Polars also supports CSV, Parquet, JSON, IPC (Arrow), and SQL databases.

Installation

To add Polars to your Rust project, include it as a dependency in your Cargo.toml:

[dependencies]
polars = "0.44.0"  # Check crates.io for the latest version

If you need additional features—like string regex support, date handling, or SQL integration—enable them with feature flags:

[dependencies]
polars = { version = "0.44.0", features = ["lazy", "strings", "dtype-date", "dtype-datetime", "sql", "parquet", "csv"] }

Key features:

  • lazy — lazy API (essential for serious work)
  • strings — string methods (contains, replace, extract)
  • dtype-date, dtype-datetime — date type support
  • sql — SQL interface
  • parquet — Parquet read/write
  • csv — CSV read/write
  • is_in, rank, moment — additional methods

After adding the dependency, run:

cargo build

Quick Start

Here's a minimal working example that creates a DataFrame and runs a simple query:

use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a DataFrame from vectors
    let df = df! [
        "name" => &["Alice", "Bob", "Charlie"],
        "age" => &[25, 30, 35],
        "salary" => &[50000, 60000, 70000]
    ]?;

    println!("Original DataFrame:\n{:?}", df);

    // Filter: who is older than 28
    let filtered = df.clone().lazy()
        .filter(col("age").gt(lit(28)))
        .collect()?;

    println!("Filtered (age > 28):\n{:?}", filtered);

    // Aggregation: average salary
    let avg_salary = df.clone().lazy()
        .select([col("salary").mean().alias("avg_salary")])
        .collect()?;

    println!("Average salary:\n{:?}", avg_salary);

    Ok(())
}

This code creates a table with three columns, filters rows by age, and calculates the average salary. Notice the use of lazy()—even for simple operations, it's recommended to build efficient query plans.

Recommendations