polars.SQLContext.register_globals#

SQLContext.register_globals(
n: int | None = None,
*,
all_compatible: bool = True,
) Self[source]#

Register all frames (lazy or eager) found in the current globals scope.

Automatically maps variable names to table names.

Parameters:
n

Register only the most recent “n” frames.

all_compatible

Control whether we also register pandas DataFrame, Series, and pyarrow Table and RecordBatch objects. If False, only Polars classes are registered with the SQL engine.

Examples

>>> df1 = pl.DataFrame({"a": [1, 2, 3], "b": ["x", None, "z"]})
>>> df2 = pl.DataFrame({"a": [2, 3, 4], "c": ["t", "w", "v"]})

Register frames directly from variables found in the current globals scope:

>>> ctx = pl.SQLContext(register_globals=True)
>>> ctx.tables()
['df1', 'df2']

Query using the register variable/frame names

>>> ctx.execute(
...     "SELECT a, b, c FROM df1 LEFT JOIN df2 USING (a) ORDER BY a DESC"
... ).collect()
shape: (3, 3)
┌─────┬──────┬──────┐
│ a   ┆ b    ┆ c    │
│ --- ┆ ---  ┆ ---  │
│ i64 ┆ str  ┆ str  │
╞═════╪══════╪══════╡
│ 3   ┆ z    ┆ w    │
│ 2   ┆ null ┆ t    │
│ 1   ┆ x    ┆ null │
└─────┴──────┴──────┘