Polars
32 steps in 10 sets of Python.
Polars is the dataframe library that took the lessons from pandas and started again in Rust. The expression API is the thing to learn: you describe a computation and the engine decides how to run it, which is closer to SQL than to pandas.
Series and frames, then expressions, filtering, aggregation, reshaping and joins. Text, lists and structs get their own set because nested data is where Polars pulls ahead. Then time series, and the lazy API with its query optimizer, which is the reason to be here at all.
Series & frames
- SeriesA Polars Series is a typed, named column backed by Arrow memory.
- Polars DataFramesBuilding a DataFrame and inspecting it.
- Reading CSVread_csv loads eagerly; scan_csv defers so the query planner can help.
- Types and castingPolars types are explicit: cast deliberately, and strict by default.
Expressions
- Polars expressionsThe expression API that powers every transform.
- Choosing columnsselect takes expressions, so a column set can be described rather than listed.
- Adding columnswith_columns evaluates expressions against the frame and returns a new one.
- Conditional expressionswhen/then/otherwise is the vectorized if, and it chains.
Filtering & sorting
- Filter and selectChoosing rows and columns from a DataFrame.
- Sorting and uniquenesssort by several columns, take the top rows, or drop duplicates.
- Handling nullsFilling and dropping missing values in Polars.
Aggregation
- Group by and aggregateSummarizing groups with aggregate expressions.
- Aggregating several waysgroup_by().agg() takes a list of expressions, each named.
- Window expressionsover computes per group but keeps every row, which agg does not.
Reshaping & joining
- Joining DataFramesCombining two frames on a shared key.
- Joining on nearest timejoin_asof matches each left row to the most recent right row.
- Pivotingpivot turns a key column into columns, aggregating where keys repeat.
- Unpivotingunpivot is the inverse: wide columns become variable and value rows.
- Concatenating framesconcat stacks frames vertically, or glues them side by side.
Text, lists & structs
- Polars string expressionsThe str namespace for text columns in Polars.
- List columnsA column can hold lists, and the list namespace works on them element-wise.
- Struct columnsA struct packs several fields into one column, and unnest spreads them out.
Time series
- Dates and timesThe dt namespace does the temporal work, on real Date and Datetime types.
- Grouping over timegroup_by_dynamic buckets rows by a time window, like a resample.
Lazy & SQL
- Lazy framesDeferring work so Polars can optimize the whole query.
- The lazy engineA LazyFrame builds a plan; collect runs it after the optimizer rewrites it.
- SQL over framesPolars will run SQL against registered frames, planner and all.
- Expressions beat map_elementsmap_elements drops into Python per row: correct, but the slow path.
Reading & writing
- Writing frames outParquet keeps the types; CSV keeps the readers; sink streams to disk.
- Talking to numpy and pandasArrow underneath means conversions are cheap, and sometimes zero-copy.
Encore
- pl_sales_report.pyBuild a monthly sales summary from raw transactions.
- pl_pipeline.pyA lazy Polars pipeline: scan, clean, window, aggregate, join and write.