Frames¶
ur.EdgeFrame¶
The graph: a table in which each row is one directed edge.
| Parameter | Description |
|---|---|
data |
Row dicts, a column dict, a polars.DataFrame, a pandas.DataFrame, a pyarrow.Table, a pyarrow.RecordBatch, or any object with __arrow_c_stream__. |
src |
The name of the source column. |
dst |
The name of the destination column. |
on_null |
"error" raises if src or dst is null in any row. "drop" removes those rows and logs a warning with the count. |
The src and dst columns must have the same type: int64 or string.
Properties¶
| Property | Description |
|---|---|
edges.src_col |
The name of the source column. |
edges.dst_col |
The name of the destination column. |
Methods¶
| Method | Returns | Description |
|---|---|---|
edges.nodes() |
NodeFrame |
The graph's node set, as one id column. Lazy. It has the rows with_columns(degree=ur.degree(edges)) has, in the same order, without the column: over a filtered frame or a traversal result the parent graph's whole node set, and after a reshaping step the nodes of its rows. For only the nodes a view touches, use ur.describe(frame, nodes="touched"). |
edges.reverse() |
EdgeFrame |
The same rows with the src and dst roles swapped. No data moves. |
edges.collect() |
MaterializedFrame |
The edge rows, after the frame's steps. |
EdgeFrame also has every shared method.
ur.NodeFrame¶
A table of node attributes, with one designated id column.
| Parameter | Description |
|---|---|
data |
The same input kinds as EdgeFrame. |
id |
The name of the id column. |
Properties¶
| Property | Description |
|---|---|
nodes.id_col |
The name of the id column. |
NodeFrame has every shared method.
Shared methods¶
Both frame types have these methods. Each lazy method returns a new frame with one more step in its plan, after checking the step against the frame: a step that cannot run raises on the line that adds it (see Errors).
The steps run in the order you write them, as in Polars. Each one sees the rows and column names the steps before it leave, so a step after rename uses the new names and a step after select sees only the selected columns.
Without a sort, rows come back in input order, the same on every run and every machine. On a plain frame that is the row order. After a graph algorithm it is the attribute table's row order, or node order for an edges.nodes() frame; after a traversal it is the traversal's own output order. A join returns the left rows in order, each followed by its matches in the right frame's order; how="right" is the mirror image, and how="outer" adds the unmatched right rows last, in their order. group_by().agg() and distinct() return each group or distinct row at the position of its first input row, and sample keeps the picked rows in input order. head(n) keeps the first n rows of that order.
Columns¶
| Method | Description |
|---|---|
.with_columns(**exprs) |
Adds one column per keyword. Each value is an expression or an algorithm, computed over the rows the steps before it leave. A keyword that names an existing column replaces it in place; other columns stay. An algorithm's column attaches to the rows by the node id column. On an EdgeFrame an algorithm is not allowed: put graph columns on edges.nodes(). |
.select(*columns) |
Keeps only the given columns, in the given order. Each item is a name, ur.col(name), or an expression, named by its .alias() or else by its first column. |
.rename({old: new, ...}) |
Renames columns. |
Rows¶
| Method | Description |
|---|---|
.filter(predicate) |
Keeps the rows where the expression is true. On an EdgeFrame before nodes(), the filter selects a subgraph. |
.sort(by, *more_by, descending=False) |
Sorts by one or more columns or expressions. by is a column name, an expression, or a list of them, and more_by adds more. Later columns break ties in earlier ones. descending is one bool for every column, or a list with one bool per column. Nulls sort last for every column. A float column sorts NaN above every number, whatever its sign bit, and -0.0 equal to 0.0 (see Floats). The sort is stable: rows that tie on every column keep their input order, on every run. After a graph algorithm that is node order, or the attribute table's row order when the frame has one; after a traversal it is the traversal's own output order. |
.head(n=10) |
Keeps the first n rows. .limit(n) is the same method. |
.distinct() |
Removes duplicate rows. Floats compare in total order (floats): -0.0 equals 0.0, and every NaN equals every NaN. |
.sample(n, *, seed=None) |
Keeps n random rows, in input order. The choice is reproducible: the same seed keeps the same rows. seed=None uses a fixed default seed, so it also keeps the same rows on every call and every run; pass a different seed for a different draw. |
Groups and joins¶
| Method | Description |
|---|---|
.group_by(*keys).agg(*exprs, **named) |
Groups by one or more columns and computes aggregations. The output columns are the keys, then the aggregations. A float key groups in total order (floats): -0.0 and 0.0 are one group, so are all NaNs, and the group shows its first row's value. A float sum, mean, min or max folds each group's values in input order, so the result is the same, bit for bit, on any number of cores. |
.join(other, *, on, how="inner") |
An equi-join with another frame on the shared key column or columns on. how is "inner", "left", "right", "outer" (or "full"), "semi" or "anti". other is any frame, a traversal or another join included. Any number of joins compose with the other steps, which run as written. A null key matches nothing; a float key matches in total order (floats). |
Inspection¶
| Method | Description |
|---|---|
.explain(physical=False) |
The plan as text: the steps, the schema, and the DataFusion plans a collect runs. Runs nothing. See below. |
.schema() |
The schema collect() returns, as a pyarrow.Schema: column names, types and nullability. Runs nothing; a frame over a scan reads the file's schema (see below). |
Execution¶
| Method | Description |
|---|---|
.collect() |
Runs the plan. Returns a MaterializedFrame. |
.to_polars() |
collect(), then a polars.DataFrame. Needs the polars extra. |
.to_arrow() |
collect(), then a pyarrow.Table. |
.to_dicts() |
collect(), then a list of row dicts. |
.sink_parquet(path, **opts) |
collect(), then write a Parquet file. opts go to pyarrow.parquet.write_table. |
.sink_csv(path) |
collect(), then write a CSV file. |
Some combinations of steps do not run yet. See Limits.
explain()¶
explain() returns text in three parts:
- The plan: the frame's steps, last step first, one per line, each written as its method call. A graph column, traversal or
describelists the edge frame it runs over indented under it (edges:), and ajoinits right side (right:). - Schema: the columns
collect()returns, with their Arrow types, markednot nullwhere they hold no nulls by construction. - DataFusion plans: the DataFusion logical plans a collect executes, in order, each optimized. A collect runs one plan for each stretch of steps between the points where it materializes rows (a graph algorithm, a sort, a join input), so a plan can have several; steps that run directly on materialized rows (a
filter, ahead) show no plan of their own.explain(physical=True)adds each one's physical plan.
explain() reads no rows and builds no graph index; a scan reads its file's schema. On a frame that does not collect as it is, the DataFusion part says why. The text is for reading: its format is not a stable API and may change between versions. Use schema() for the columns programmatically.
schema() and explain() cache nothing they read from a scan: each call lists the scan's files and reads their schema (a Parquet file's footer) again, so the result follows the files as they are now. For an s3://, gs://, az:// or http(s):// path that is a network round trip per call.
ur.MaterializedFrame¶
The result of collect(). It holds a chunked pyarrow.Table. You do not construct it yourself.
| Member | Description |
|---|---|
.columns |
The column names, as a list. |
.to_arrow() |
The pyarrow.Table. No copy. |
.to_polars() |
A polars.DataFrame. No copy. Needs the polars extra. |
.to_dicts() |
A list of row dicts. |
.sink_parquet(path, **opts) |
Writes a Parquet file. |
.sink_csv(path) |
Writes a CSV file. |
len(result) |
The number of rows. |
repr(result) |
A table preview: the shape, the column names, the types, and the first 10 rows. |