phibble

Guide

phibble

phibble is a data frame library for PHP. A DataFrame is a table of named, typed columns, such as the rows of a CSV file or a database query. phibble reads such tables, filters, sorts, groups, joins, and summarizes them, and writes them back out. Its design and names follow R’s tibble and readr packages, and its verbs follow dplyr.

$penguins = DataFrame::fromCsv('penguins.csv');

$summary = $penguins
    ->filter(col('body_mass')->isNa()->not())
    ->groupBy(['species', 'sex'])
    ->summarize([
        'n' => n(),
        'mass' => mean(col('body_mass')),
        'flipper' => median(col('flipper_len')),
    ]);

echo $summary;
# A tibble: 8 × 5
# Groups:   species [3]
  species   sex        n  mass flipper
  <chr>     <chr>  <int> <dbl>   <dbl>
1 Adelie    female    73 3369.    188 
2 Adelie    male      73 4043.    193 
3 Adelie    <NA>       5 3540     186 
4 Chinstrap female    34 3527.    192 
5 Chinstrap male      34 3939.    200.
6 Gentoo    female    58 4680.    212 
7 Gentoo    male      61 5485.    221 
8 Gentoo    <NA>       4 4588.    216

Tables

  • Every column has one type: float, int, bool, string, factor, date, datetime, or list. Types are guessed as a table loads, or declared per column.
  • PHP null is a missing value (R’s NA) in every type.
  • A DataFrame never changes in place. Every method returns a new table.
  • Tables load from CSV, JSON, PDO queries, Laravel and Doctrine query builders, and Laravel collections, and write to CSV and JSON.

The types guide and reading and writing data cover these in detail.

Backends

Three classes implement the Phibble\Contracts\Table interface, and all three are part of phibble:

  • Phibble\DataFrame holds the data in PHP arrays.
  • Phibble\Sql\SqlTable compiles the verbs to SQL for SQLite, PostgreSQL, and MySQL.
  • Phibble\Polars\PolarsTable runs the verbs in Polars. It needs the php_polars extension from phpolars.

A step that a backend cannot run itself, such as a PHP closure, runs in a DataFrame. The backends page describes how results can still differ between backends.

Other packages

The other PHPstats packages read and return phibble tables:

  • phplyr provides dplyr and tidyr as PHP functions and method chains.
  • phit fits statistical models, reading their data through DataFrame::toModelData(). Its extension also speeds up phibble’s CSV reader.
  • phpolars provides the Polars extension that PolarsTable uses.
  • phiz plots a DataFrame directly.
  • CmdStanPHP accepts a DataFrame as model data and returns draws and summaries as tables.

Install

composer require phpstats/phibble

Requirements:

  • PHP 8.3, 8.4, or 8.5. CI tests all three.
  • The mbstring extension.
  • The PDO extension.

Optional:

  • The phit extension reads CSV natively. fromCsv() uses phit 0.1.1 or later, and the chunked readers use phit 0.2.0 or later.
  • The php_polars extension enables PolarsTable.
  • illuminate/database enables the Laravel macros.

Pipelines can use PHP 8.5’s |> operator. Method chains work on every supported version.

From phpstats/phibble@36d0f3f.