Get the result out¶
collect() returns a MaterializedFrame. It holds an Arrow table. From there the data goes to polars, to pyarrow, to Python values, or to a file. No step copies the data unless it must.
import ursa as ur
edges, nodes = ur.datasets.load_karate(with_nodes=True)
result = nodes.with_columns(degree=ur.degree(edges, direction="both")).collect()
In the REPL¶
repr shows the shape, the columns with their types, and the first 10 rows.
shape: (34, 3)
┌───────┬────────┬────────┐
│ id ┆ club ┆ degree │
│ --- ┆ --- ┆ --- │
│ int64 ┆ string ┆ uint32 │
╞═══════╪════════╪════════╡
│ 0 ┆ Mr. Hi ┆ 16 │
│ 1 ┆ Mr. Hi ┆ 9 │
│ 2 ┆ Mr. Hi ┆ 10 │
│ 3 ┆ Mr. Hi ┆ 6 │
│ 4 ┆ Mr. Hi ┆ 3 │
│ 5 ┆ Mr. Hi ┆ 4 │
│ 6 ┆ Mr. Hi ┆ 4 │
│ 7 ┆ Mr. Hi ┆ 4 │
│ 8 ┆ Mr. Hi ┆ 5 │
│ 10 ┆ Mr. Hi ┆ 3 │
│ … ┆ … ┆ … │
└───────┴────────┴────────┘
To Arrow¶
To polars¶
With the polars extra installed:
shape: (3, 3)
┌─────┬─────────┬────────┐
│ id ┆ club ┆ degree │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ u32 │
╞═════╪═════════╪════════╡
│ 0 ┆ Mr. Hi ┆ 16 │
│ 32 ┆ Officer ┆ 12 │
│ 33 ┆ Officer ┆ 17 │
└─────┴─────────┴────────┘
To Python values¶
To a file¶
sink_parquet and sink_csv write the result. Keyword arguments to sink_parquet go to pyarrow.parquet.write_table.
result.sink_parquet("karate_degree.parquet", compression="zstd")
result.sink_csv("karate_degree.csv")
back = ur.read_nodes("karate_degree.parquet", id="id")
print(back.columns, len(back.to_dicts()))
Shortcuts on a lazy frame¶
A lazy frame has the same methods. Each one calls collect() for you.