Guide
phpolars
phpolars is a PHP extension, written in Rust, that runs Polars inside PHP. Polars is a columnar data frame library: a DataFrame holds typed columns, and expressions such as col('units')->mul(col('price')) describe computations that Polars runs on its own threads, outside PHP. A query that reads a CSV file, filters it, and groups it runs in Polars. PHP values are created only when the code asks for them, for example with toDicts().
The API follows py-polars, Polars’ Python package, with camelCase method names and snake_case functions. The test suite compares 314 cases with py-polars 1.43.2, and all of them match; Compatibility and limitations lists what that comparison covers and what phpolars leaves out.
$sales = read_csv('sales.csv', tryParseDates: true);
$top = $sales
->filter(col('units')->gt(0))
->withColumns(col('units')->mul(col('price'))->alias('revenue'))
->groupBy('product', maintainOrder: true)
->agg(col('revenue')->sum()->round(2), ['orders' => len()], col('price')->mean()->round(2)->alias('avg_price'))
->sort('revenue', descending: true);
echo $top;shape: (4, 4)
┌─────────┬─────────┬────────┬───────────┐
│ product ┆ revenue ┆ orders ┆ avg_price │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ u32 ┆ f64 │
╞═════════╪═════════╪════════╪═══════════╡
│ cherry ┆ 1307.38 ┆ 44 ┆ 4.09 │
│ banana ┆ 1285.76 ┆ 48 ┆ 4.35 │
│ apple ┆ 1227.68 ┆ 42 ┆ 4.38 │
│ date ┆ 1153.58 ┆ 41 ┆ 4.46 │
└─────────┴─────────┴────────┴───────────┘
What it includes
DataFrameandLazyFramewith py-polars’ methods:select,withColumns,filter,sort,groupBy/agg,join,unique,pivot,unpivot,explode,describe, and more.- Expressions with arithmetic, three-valued logic, aggregations, window functions (
over),when/then/otherwise, and thestr,dt,list,struct, andnamenamespaces. Series, with the same expression methods.- CSV, Parquet, JSON, NDJSON, and Arrow IPC reading and writing, with lazy scans and streaming sinks, and reading and writing through PDO.
- SQL queries over data frames.
- A thread-pool cap for PHP-FPM (
php_polars.max_threads).
The phpolars API or phibble and phplyr
phpolars can be used directly or through phibble and phplyr.
- The phpolars API (
Phpolars\DataFrame,LazyFrame,Series, and expressions) follows Polars and its semantics. A missing value isnull, and aggregates skip it. - phibble’s
Phibble\Polars\PolarsTableadapter runs phibble tables and phplyr pipelines in Polars. It belongs to phibble, and phpolars supplies the extension that it runs on. The pipeline keeps dplyr’s semantics, in whichnullis R’sNA, and the same code also runs on phibble’s in-memoryDataFrameand on SQL tables.
phibble and phplyr covers the adapter and the differences between the two sets of semantics.