phpstats / phpolars
Polars data frames for PHP, as a native Rust extension
Package info
Language:Rust
Type:php-ext
Ext name:ext-php_polars
pkg:composer/phpstats/phpolars
Requires
- php: >=8.3 <8.6
Requires (Dev)
- squizlabs/php_codesniffer: ^3.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
phpolars brings Polars data frames to PHP as a native extension written in Rust with ext-php-rs. The API follows py-polars: the same classes and methods, with camelCase method names and snake_case functions.
use function Phpolars\{col, len, read_csv}; // Palmer penguins: https://github.com/phpstats/datasets#penguins $penguins = read_csv( 'https://raw.githubusercontent.com/phpstats/datasets/main/csv/penguins.csv', nullValues: 'NA', ); $summary = $penguins ->filter(col('body_mass_g')->isNotNull()) ->groupBy(['species', 'island'], maintainOrder: true) ->agg(len()->alias('penguins'), col('body_mass_g')->mean()->round()->alias('mass_g')) ->sort('mass_g', descending: true); print_r($summary->toDicts());
phpolars works on its own or through phibble and phplyr. phibble's Phibble\Polars\PolarsTable adapter, part of phibble, runs phibble tables and phplyr pipelines on this extension with dplyr's semantics.
Requirements
- PHP 8.3, 8.4, or 8.5
- A non-thread-safe (NTS) PHP build
- Linux or macOS
- PIE
Prebuilt binaries cover Linux with glibc 2.34 or later on x86_64 and arm64. Other platforms, including macOS, build from source, which needs a stable Rust toolchain and libclang.
Installing
pie install phpstats/phpolars
php -r 'echo phpversion("php_polars"), PHP_EOL;'
The second command prints the installed version. Restart PHP-FPM and queue workers after installing. Deployment covers manual installs, Ubuntu and Laravel Forge, Laravel Herd, threads under PHP-FPM, and memory.
Documentation
- Getting started
- Coming from py-polars
- phibble and phplyr, including how Polars' missing values differ from dplyr's
- Reference
- Compatibility and limitations
- Performance
Binary size
Polars is large. Release builds use the dist profile, which applies fat LTO, one codegen unit, and symbol stripping. Optional Cargo features gate the formats and data that not every installation needs:
| Feature | Provides |
|---|---|
parquet |
read_parquet, scan_parquet, writeParquet, sinkParquet |
json |
read_json, read_ndjson, writeJson, writeNdjson |
ipc |
Arrow IPC (Feather) reading and writing; also enables parquet, which Polars 0.55's IPC reader needs |
fmt |
the table layout used by __toString() |
sql |
SQLContext, DataFrame::sql(), LazyFrame::sql(), and Phpolars\sql() |
All features are on by default. Build a smaller extension with, for example, cargo build --profile dist --no-default-features --features parquet.
Development
Development covers building from a checkout, the test suites, the py-polars fixtures, the docs, and the IDE stubs in stubs/phpolars.php.
License
MIT