alias

pydiverse.transform.alias(
new_name: str | None = None,
*,
keep_col_refs: bool = False,
) Pipeable[source]

Changes the name of the current table and allows subqueries in SQL.

Parameters:
  • new_name – The new name assigned to the table. If this is None, the table retains its previous name.

  • keep_col_refs – Whether column references are reset. If keep_col_refs=False, the resulting table is not considered to be derived from the input table, i.e. one cannot use columns from the input table in subsequent operations on the result table. However, the reset of column references is necessary before performing a self-join.

Examples

A self join without applying alias before raises an exception:

>>> t = pdt.Table({"a": [4, 2, 1, 4], "b": ["l", "g", "uu", "--   r"]})
>>> t >> join(t, t.a == t.a, how="inner", suffix="_right")
ValueError: table `<pydiverse.transform._internal.backend.polars.PolarsImpl object
at 0x13f86d510>` occurs twice in the table tree.
hint: To join two tables derived from a common table, apply `>> alias()` to one of
them before the join.

By applying alias to the right table and storing the result in a new variable, the self join succeeds.

>>> (
...     t
...     >> join(s := t >> alias(), t.a == s.a, how="inner", suffix="_right")
...     >> show()
... )
shape: (6, 4)
┌─────┬────────┬─────────┬─────────┐
│ a   ┆ b      ┆ a_right ┆ b_right │
│ --- ┆ ---    ┆ ---     ┆ ---     │
│ i64 ┆ str    ┆ i64     ┆ str     │
╞═════╪════════╪═════════╪═════════╡
│ 4   ┆ l      ┆ 4       ┆ l       │
│ 4   ┆ --   r ┆ 4       ┆ l       │
│ 2   ┆ g      ┆ 2       ┆ g       │
│ 1   ┆ uu     ┆ 1       ┆ uu      │
│ 4   ┆ l      ┆ 4       ┆ --   r  │
│ 4   ┆ --   r ┆ 4       ┆ --   r  │
└─────┴────────┴─────────┴─────────┘