Creating and loading tables
On this page
- Functions
-
DataFrame
- DataFrame::fromRows()
- DataFrame::fromColumns()
- DataFrame::fromCsv()
- DataFrame::fromCsvString()
- DataFrame::fromCsvChunked()
- DataFrame::csvChunks()
- DataFrame::fromJson()
- DataFrame::fromPdo()
- DataFrame::fromQuery()
- DataFrame::fromCollection()
- DataFrame::fromSource()
- DataFrame::tibble()
- DataFrame::tribble()
- DataFrame::asTibble()
- DataFrame::enframe()
- SqlTable
- PolarsTable
Functions and static methods that create a table from PHP values, files, queries, or other tables.
Functions
tibble()
tibble(mixed ...$columns): DataFrameBuild a DataFrame from named arguments, one per column, as tibble’s tibble(). Same rules as DataFrame::tibble().
| Parameter | Type | Default | Description |
|---|---|---|---|
...$columns |
mixed |
Function in Phibble
Returns DataFrame
tribble()
tribble(array $names, mixed ...$cells): DataFrameBuild a DataFrame from column names, then rows, as tibble’s tribble(). Same rules as DataFrame::tribble().
| Parameter | Type | Default | Description |
|---|---|---|---|
$names |
array |
||
...$cells |
mixed |
Function in Phibble
Returns DataFrame
as_tibble()
as_tibble(mixed $data, string $nameRepair = 'check_unique'): DataFrameConvert tables, rows, columns, or collections into a DataFrame, as tibble’s as_tibble(). Same rules as DataFrame::asTibble().
| Parameter | Type | Default | Description |
|---|---|---|---|
$data |
mixed |
||
$nameRepair |
string |
‘check_unique’ |
Function in Phibble
Returns DataFrame
enframe()
enframe( array $values, ?string $name = 'name', string $value = 'value',): DataFrameAn array as a two-column DataFrame of names and values, as tibble’s enframe(). Same rules as DataFrame::enframe().
| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array |
||
$name |
?string |
‘name’ |
|
$value |
string |
‘value’ |
Function in Phibble
Returns DataFrame
tbl()
tbl(PDO $pdo, string $from, array $bindings = []): SqlTableA lazy SqlTable, as dbplyr’s tbl(). Pass a table name, or a SELECT or WITH query with bindings.
tbl($pdo, ‘visits’); tbl($pdo, ‘SELECT * FROM visits WHERE year = ?’, [2024]);
| Parameter | Type | Default | Description |
|---|---|---|---|
$pdo |
PDO |
||
$from |
string |
A table or view name, or a SELECT or WITH query. | |
$bindings |
array<int|string, mixed> |
[] |
Positional (?) or named (:name) bindings, for a query only. |
Function in Phibble
Returns SqlTable
Throws InvalidArgumentException: when bindings come with a table name.
polars()
polars(mixed $data): PolarsTableA PolarsTable from any Table, or from anything as_tibble() accepts. Requires the php_polars extension.
polars(DataFrame::fromCsv('sales.csv'))->filter(col('units')``->gt(0));
| Parameter | Type | Default | Description |
|---|---|---|---|
$data |
mixed |
Function in Phibble
Returns PolarsTable
Throws RuntimeException: when the php_polars extension is not loaded.
DataFrame
DataFrame::fromRows()
DataFrame::fromRows( iterable $rows, array $types = [], bool $parseStrings = false,): DataFrameBuild a DataFrame from rows. Column order follows first appearance; keys missing from a row are NA.
| Parameter | Type | Default | Description |
|---|---|---|---|
$rows |
iterable<array<string, mixed>|object> |
||
$types |
array<string, Type|string> |
[] |
Explicit types by column name. |
$parseStrings |
bool |
false |
Parse all-string columns as text (as fromCsv does). |
Static method of Phibble\DataFrame
Returns DataFrame
Examples Getting started
DataFrame::fromColumns()
DataFrame::fromColumns( object|array $columns, array $types = [],): DataFrameBuild a DataFrame from columns: name => list of values, or name => Column.
An object with a public toColumns() method, such as phit’s Phit\Table or another phibble table, is read through that method.
| Parameter | Type | Default | Description |
|---|---|---|---|
$columns |
array<string, list<mixed>|Column>|object |
||
$types |
array<string, Type|string> |
[] |
Explicit types by column name. |
Static method of Phibble\DataFrame
Returns DataFrame
Throws InvalidArgumentException: when an object has no toColumns() method, or it returns something other than an array.
Examples Types, Getting started
DataFrame::fromCsv()
DataFrame::fromCsv( mixed $file, array|string $types = [], string $delimiter = ',', array $na = ['', 'NA'], mixed ...$options,): DataFrameRead delimited text like readr::read_csv(): a path (“.gz” is decompressed), an http:// or https:// URL (downloaded to a temporary file first), or an open stream. Types are guessed from up to guessMax rows; “” and “NA” are NA. More readr options go as named arguments; see Phibble\Io\CsvOptions (colNames, select, skip, nMax, quote, comment, trimWs, skipEmptyRows, guessMax, nameRepair, decimalMark, groupingMark, escapeDouble, escapeBackslash).
Problems (unparseable values, ragged rows) raise one E_USER_WARNING; the data is still returned, with NA where values failed.
| Parameter | Type | Default | Description |
|---|---|---|---|
$file |
string|resource |
||
$types |
string|array<int|string, mixed> |
[] |
readr col_types: “icdlDTfn_?” or name => spec. |
$delimiter |
string |
‘,’ |
|
$na |
list<string> |
[’‘, ’NA’] |
|
...$options |
mixed |
Static method of Phibble\DataFrame
Returns DataFrame
Examples Getting started, Reading and writing data, Expressions
DataFrame::fromCsvString()
DataFrame::fromCsvString( string $csv, array|string $types = [], string $delimiter = ',', array $na = ['', 'NA'], mixed ...$options,): DataFrameRead delimited text from a string; options as fromCsv().
| Parameter | Type | Default | Description |
|---|---|---|---|
$csv |
string |
||
$types |
string|array<int|string, mixed> |
[] |
|
$delimiter |
string |
‘,’ |
|
$na |
list<string> |
[’‘, ’NA’] |
|
...$options |
mixed |
Static method of Phibble\DataFrame
Returns DataFrame
Examples Reading and writing data, Types
DataFrame::fromCsvChunked()
DataFrame::fromCsvChunked( mixed $file, callable $callback, int $chunkSize = 10000, array|string $types = [], string $delimiter = ',', array $na = ['', 'NA'], mixed ...$options,): voidRead delimited text in chunks, as readr::read_delim_chunked(): each chunk of at most $chunkSize rows is passed to $callback with the 1-based index of its first row. Returning false from the callback stops reading. Memory follows the chunk size, not the file’s.
Types not given are guessed from the first chunk (guessMax defaults to the chunk size, as in readr) and every later chunk is parsed as those types. Give types, for example [‘.default’ => ‘c’], to skip guessing. Factors without levels take their levels from each chunk. Each chunk’s problems raise their own warning, with rows numbered from the start of the file. Options are those of fromCsv(), and $file may be a URL as there. With the phit extension loaded, the chunks are read natively by Phit\CsvReader.
| Parameter | Type | Default | Description |
|---|---|---|---|
$file |
string|resource |
||
$callback |
callable(DataFrame, int): (bool|null|void) |
||
$chunkSize |
int |
10000 |
|
$types |
string|array<int|string, mixed> |
[] |
|
$delimiter |
string |
‘,’ |
|
$na |
list<string> |
[’‘, ’NA’] |
|
...$options |
mixed |
Static method of Phibble\DataFrame
Examples Reading and writing data
DataFrame::csvChunks()
DataFrame::csvChunks( mixed $file, int $chunkSize = 10000, array|string $types = [], string $delimiter = ',', array $na = ['', 'NA'], mixed ...$options,): GeneratorThe chunks fromCsvChunked() reads, as a generator keyed by the 1-based index of each chunk’s first row.
$kept = []; foreach (DataFrame::csvChunks('feed.tsv', delimiter: "\t", types: ['.default' => 'c']) as $chunk) { $kept[] = $chunk->filter(col('category')``->eq('running')); } $shoes = DataFrame::concat($kept);
| Parameter | Type | Default | Description |
|---|---|---|---|
$file |
string|resource |
||
$chunkSize |
int |
10000 |
|
$types |
string|array<int|string, mixed> |
[] |
|
$delimiter |
string |
‘,’ |
|
$na |
list<string> |
[’‘, ’NA’] |
|
...$options |
mixed |
Static method of Phibble\DataFrame
Returns Generator<int, DataFrame>
Examples Reading and writing data
DataFrame::fromJson()
DataFrame::fromJson( string $json, array $types = [], ?string $orient = null,): DataFrameRead JSON: a list of records ([{“a”: 1}, …]) or an object of columns ({“a”: [1, 2]}). The shape is detected unless $orient is ‘records’ or ‘columns’. Missing keys and nulls are NA; nested arrays and objects become list-columns.
| Parameter | Type | Default | Description |
|---|---|---|---|
$json |
string |
||
$types |
array<string, Type|string> |
[] |
|
$orient |
?string |
null |
Static method of Phibble\DataFrame
Returns DataFrame
Examples Reading and writing data
DataFrame::fromPdo()
DataFrame::fromPdo( PDO $pdo, string $sql, array $bindings = [], array $types = [],): DataFrameRun a query and load the result.
| Parameter | Type | Default | Description |
|---|---|---|---|
$pdo |
PDO |
||
$sql |
string |
||
$bindings |
array<int|string, mixed> |
[] |
Positional or named. |
$types |
array<string, Type|string> |
[] |
Static method of Phibble\DataFrame
Returns DataFrame
Examples Reading and writing data
DataFrame::fromQuery()
DataFrame::fromQuery( object $builder, ?object $connection = null, array $types = [],): DataFrameLoad the result of a Laravel query builder (query or Eloquent) or a Doctrine DBAL QueryBuilder. Neither library is required.
| Parameter | Type | Default | Description |
|---|---|---|---|
$builder |
object |
||
$connection |
?object |
null |
|
$types |
array<string, Type|string> |
[] |
Static method of Phibble\DataFrame
Returns DataFrame
DataFrame::fromCollection()
DataFrame::fromCollection( Traversable|object|array $collection, array $types = [],): DataFrameBuild a DataFrame from a Laravel Collection (or anything with all() or toArray(), or any iterable). Items may be arrays, Eloquent models or other objects with toArray(), or plain objects. Laravel is not needed.
| Parameter | Type | Default | Description |
|---|---|---|---|
$collection |
iterable<mixed>|object |
||
$types |
array<string, Type|string> |
[] |
Static method of Phibble\DataFrame
Returns DataFrame
Examples Reading and writing data
DataFrame::fromSource()
DataFrame::fromSource(Source $source): DataFrameRead a Phibble\Source\Source, such as a CsvSource or PdoSource, into a DataFrame.
| Parameter | Type | Default | Description |
|---|---|---|---|
$source |
Source |
Static method of Phibble\DataFrame
Returns DataFrame
DataFrame::tibble()
DataFrame::tibble( array $columns, string $nameRepair = 'check_unique',): DataFrameBuild a DataFrame column by column, as tibble’s tibble().
- A list is a column; a scalar (or null) is repeated to the common length. Only length-1 values are recycled.
- A closure receives the DataFrame built so far and returns a list or a scalar, so columns can be computed from earlier ones.
- A Table under an integer key is spliced in column by column.
| Parameter | Type | Default | Description |
|---|---|---|---|
$columns |
array<string|int, mixed> |
||
$nameRepair |
string |
‘check_unique’ |
check_unique, unique, universal or minimal |
Static method of Phibble\DataFrame
Returns DataFrame
DataFrame::tribble()
DataFrame::tribble(array $names, mixed ...$cells): DataFrameBuild a DataFrame row by row, as tibble’s tribble(). Pass the column names, then either one list per row or the cells in row order.
DataFrame::tribble(['x', 'y'], [1, 'a'], [2, 'b']);
| Parameter | Type | Default | Description |
|---|---|---|---|
$names |
list<string> |
||
...$cells |
mixed |
Static method of Phibble\DataFrame
Returns DataFrame
DataFrame::asTibble()
DataFrame::asTibble( mixed $data, string $nameRepair = 'check_unique',): DataFrameConvert tables, rows, columns, collections, or other iterables into a DataFrame, as tibble’s as_tibble().
- A Table is collected.
- A list of associative arrays (or objects) is rows.
- An associative array of lists is columns.
- An object with
toColumns(), such as phit’sPhit\Table, is read as columns. - An object with
all()ortoArray()(a Laravel Collection) is unwrapped.
| Parameter | Type | Default | Description |
|---|---|---|---|
$data |
mixed |
||
$nameRepair |
string |
‘check_unique’ |
check_unique, unique, universal or minimal |
Static method of Phibble\DataFrame
Returns DataFrame
DataFrame::enframe()
DataFrame::enframe( array $values, ?string $name = 'name', string $value = 'value',): DataFrameAn array as a two-column DataFrame of names and values, as tibble’s enframe(). A list gets 1-based integer names, as in R.
| Parameter | Type | Default | Description |
|---|---|---|---|
$values |
array<mixed> |
||
$name |
?string |
‘name’ |
|
$value |
string |
‘value’ |
Static method of Phibble\DataFrame
Returns DataFrame
SqlTable
SqlTable::table()
SqlTable::table(PDO $pdo, string $name): SqlTableA table (or view) by name.
| Parameter | Type | Default | Description |
|---|---|---|---|
$pdo |
PDO |
||
$name |
string |
Static method of Phibble\Sql\SqlTable
Returns SqlTable
SqlTable::query()
SqlTable::query(PDO $pdo, string $sql, array $bindings = []): SqlTableAny SELECT query with positional (?) or named (:name) bindings.
| Parameter | Type | Default | Description |
|---|---|---|---|
$pdo |
PDO |
||
$sql |
string |
||
$bindings |
array<int|string, mixed> |
[] |
Static method of Phibble\Sql\SqlTable
Returns SqlTable
SqlTable::fromQuery()
SqlTable::fromQuery( object $builder, ?object $connection = null,): SqlTableA Laravel or Doctrine DBAL query builder, kept lazy.
| Parameter | Type | Default | Description |
|---|---|---|---|
$builder |
object |
||
$connection |
?object |
null |
Static method of Phibble\Sql\SqlTable
Returns SqlTable
SqlTable::fromSource()
SqlTable::fromSource(PdoSource $source): SqlTableA lazy table over a PdoSource’s query and bindings.
| Parameter | Type | Default | Description |
|---|---|---|---|
$source |
PdoSource |
Static method of Phibble\Sql\SqlTable
Returns SqlTable
PolarsTable
PolarsTable::fromTable()
PolarsTable::fromTable(Table $table): PolarsTableCopy any phibble table into Polars, keeping types, groups and factor levels.
| Parameter | Type | Default | Description |
|---|---|---|---|
$table |
Table |
Static method of Phibble\Polars\PolarsTable
Returns PolarsTable
PolarsTable::fromPolars()
PolarsTable::fromPolars( Phpolars\DataFrame|Phpolars\LazyFrame $df,): PolarsTableWrap a Polars DataFrame; a LazyFrame is collected.
| Parameter | Type | Default | Description |
|---|---|---|---|
$df |
Phpolars|Phpolars |
Static method of Phibble\Polars\PolarsTable
Returns PolarsTable