phpolars

Functions and methods

phpolars provides functions in the Phpolars namespace, selectors in Phpolars\cs, and classes whose methods build and run queries. Import functions with use function Phpolars\{col, lit, read_csv}; and selectors with use Phpolars\cs;. Each name links to its signature and notes.

Functions

Read files and databases

Function Purpose
read_csv() Read a CSV file into a DataFrame.
scan_csv() Scan a CSV file lazily as a LazyFrame.
read_parquet() Read a Parquet file into a DataFrame.
scan_parquet() Scan a Parquet file lazily as a LazyFrame.
read_parquet_schema() Read the schema of a Parquet file.
read_ipc() Read an Arrow IPC file into a DataFrame.
scan_ipc() Scan an Arrow IPC file lazily as a LazyFrame.
read_ipc_schema() Read the schema of an Arrow IPC file.
read_ipc_stream() Read an Arrow IPC stream into a DataFrame.
read_ndjson() Read newline-delimited JSON into a DataFrame.
scan_ndjson() Scan newline-delimited JSON lazily as a LazyFrame.
read_json() Read a JSON array of rows into a DataFrame.
read_database() Read the result of a PDO query into a DataFrame.

Create and combine data frames

Function Purpose
from_dict() Build a DataFrame from an array of columns.
from_dicts() Build a DataFrame from a list of associative rows.
from_records() Build a DataFrame from a list of rows.
json_normalize() Flatten nested arrays into a DataFrame with one column per path.
concat() Concatenate data frames, LazyFrames, or series vertically, horizontally, or diagonally.
union() Concatenate data frames or LazyFrames without guaranteeing row order.
select() Evaluate expressions into a DataFrame without an input data frame.
sql() Run a SQL query against the given data frames.
collect_all() Collect several LazyFrames in parallel.

Columns and literals

Function Purpose
col() Refer to columns by name, by regular expression, or by data type.
lit() A literal value, optionally of a given data type.
all() Every column.
exclude() Every column except the given ones.
nth() Columns by position.
first() The first column; with column names, the first value of each.
last() The last column; with column names, the last value of each.
len() The number of rows.
row_index() A row index column, starting at 0.
element() The current element inside list()->eval() and arr() operations.
field() A struct field inside struct()->withFields().
struct_() Combine expressions into a struct column.
repeat() A value repeated n times.
ones() A column of n ones.
zeros() A column of n zeros.
sql_expr() Parse a SQL expression into an Expr.

Conditions

Function Purpose
when() Start a when()->then()->otherwise() conditional expression.
coalesce() The first non-null value across expressions, row by row.

Aggregates

Function Purpose
sum() Sum of the given columns.
mean() Mean of the given columns.
median() Median of the given columns.
min() Minimum of the given columns.
max() Maximum of the given columns.
count() Number of non-null values in the given columns.
n_unique() Number of distinct values in the given columns.
approx_n_unique() Approximate number of distinct values (HyperLogLog).
std() Sample standard deviation of a column.
var_() Sample variance of a column.
quantile() A quantile of a column.
any() Whether any value of the given boolean columns is true.
implode() Collect the values of the given columns into one list.
head() The first n values of the given columns.
tail() The last n values of the given columns.
corr() Correlation of two columns (Pearson or Spearman).
cov() Covariance of two columns.
rolling_corr() Rolling correlation of two columns.
rolling_cov() Rolling covariance of two columns.
cum_count() Cumulative count of non-null values.
cum_sum() Cumulative sum of the given columns.

Across columns

Function Purpose
all_horizontal() Logical AND across expressions, row by row.
any_horizontal() Logical OR across expressions, row by row.
sum_horizontal() Sum across expressions, row by row.
mean_horizontal() Mean across expressions, row by row.
min_horizontal() Minimum across expressions, row by row.
max_horizontal() Maximum across expressions, row by row.
cum_sum_horizontal() Cumulative sum across expressions, as a struct.
concat_str() Concatenate expressions as strings, row by row.
concat_list() Combine expressions into a list column, row by row.
concat_arr() Combine expressions into a fixed-width array column.
format() Format expressions into a string with {} placeholders.
arctan2() Two-argument arctangent of y and x.

Dates, times, and ranges

Function Purpose
date() A date from year, month, and day expressions.
datetime() A datetime from its component expressions.
time() A time of day from its component expressions.
duration() A duration from weeks, days, hours, and smaller units.
from_epoch() Convert Unix timestamps to datetimes or dates.
business_day_count() Number of business days between two dates.
date_range() A column of dates at a fixed interval.
date_ranges() A list of dates per row, between two date columns.
datetime_range() A column of datetimes at a fixed interval.
datetime_ranges() A list of datetimes per row, between two columns.
time_range() A column of times of day at a fixed interval.
time_ranges() A list of times per row, between two columns.
int_range() A column of integers from start to end.
int_ranges() A list of integers per row, between two columns.
arange() A column of integers from start to end, as int_range().
linear_space() Evenly spaced values between two bounds.
linear_spaces() Evenly spaced values per row, as a list or array.

Sorting and positions

Function Purpose
arg_sort_by() Row indices that sort by the given expressions.
arg_where() Row indices where a condition is true.
escape_regex() Escape regular expression metacharacters in a string.

Runtime

Function Purpose
version() The phpolars extension version.
polars_version() The version of the embedded Polars Rust crate.
thread_pool_size() The number of threads in the Polars thread pool.
set_random_seed() Seed Polars’ random number generator for sample() and shuffle().

Selectors

Function Purpose
cs\all() Every column.
cs\array_() Array columns, optionally of an inner type and width.
cs\binary() Binary columns.
cs\boolean() Boolean columns.
cs\by_dtype() Columns of the given data types.
cs\by_index() Columns at the given positions.
cs\by_name() Columns with the given names.
cs\categorical() Categorical columns.
cs\contains() Columns whose names contain a substring.
cs\date() Date columns.
cs\datetime() Datetime columns, optionally of a time unit and zone.
cs\decimal() Decimal columns.
cs\duration() Duration columns, optionally of a time unit.
cs\empty_() No columns.
cs\ends_with() Columns whose names end with a suffix.
cs\enum_() Enum columns.
cs\exclude() Every column except the given ones.
cs\first() The first column.
cs\float() Float columns.
cs\integer() Integer columns.
cs\last() The last column.
cs\list_() List columns, optionally of an inner type.
cs\matches() Columns whose names match a regular expression.
cs\nested() List, array, and struct columns.
cs\numeric() Integer, float, and decimal columns.
cs\signed_integer() Signed integer columns.
cs\starts_with() Columns whose names start with a prefix.
cs\string() String columns.
cs\struct_() Struct columns.
cs\temporal() Date, datetime, duration, and time columns.
cs\time() Time columns.
cs\unsigned_integer() Unsigned integer columns.

Classes and methods

Methods are instance methods unless the signature starts with static. Each class links to its full method list, grouped by task.

Class Purpose Methods
Data frames
DataFrame Data in memory; each method runs immediately. Create and convert · Inspect · Select and add columns · Filter, sort, and slice rows · Missing values · Group and aggregate · Join and combine · Reshape · SQL · Write
LazyFrame A query plan that runs, optimized, on collect(). Create and convert · Inspect · Select and add columns · Filter, sort, and slice rows · Missing values · Group and aggregate · Join and combine · Reshape · SQL · Write
GroupBy The result of DataFrame::groupBy(): agg() and per-group summaries. 17 methods
LazyGroupBy The result of LazyFrame::groupBy(). 17 methods
GroupIterator Iterates over a GroupBy with foreach: group keys and rows. 5 methods
SQLContext Registers data frames as tables and runs SQL queries. 5 methods
Columns and expressions
Series One named column of values. Arithmetic and comparison · Logic and bits · Missing values · Aggregate · Math · Windows, cumulative, and rolling · Sort, select, and deduplicate · Combine and reshape · Types, names, and replacing values · Namespaces · Create and convert
Expr A computation on columns, evaluated by select(), withColumns(), filter(), and agg(). Arithmetic and comparison · Logic and bits · Missing values · Aggregate · Math · Windows, cumulative, and rolling · Sort, select, and deduplicate · Combine and reshape · Types, names, and replacing values · Namespaces · Create and convert
When A condition from when(), awaiting then(). 1 method
Then A conditional expression, continued by when() or otherwise(). 3 methods
StrNameSpace String operations: col(...)->str(). 46 methods
DtNameSpace Date, datetime, and duration operations: col(...)->dt(). 47 methods
ListNameSpace List operations: col(...)->list(). 43 methods
ArrayNameSpace Fixed-width array operations: col(...)->arr(). 31 methods
StructNameSpace Struct field operations: col(...)->struct(). 5 methods
CatNameSpace Categorical operations: col(...)->cat(). 6 methods
BinaryNameSpace Binary operations: col(...)->bin(). 11 methods
NameNameSpace Output column naming: col(...)->name(). 8 methods
MetaNameSpace Inspection of an expression itself: col(...)->meta(). 9 methods
Types and settings
DataType Data type names as constants, and builders for parameterized types. 3 methods
Config Settings for how data frames print. 20 methods

Errors

Polars errors are thrown as subclasses of Phpolars\PolarsError in Phpolars\Exception. Types and values lists them.