lace.Engine.append_rows

Engine.append_rows(rows: Series | DataFrame | DataFrame | Dict[str, Dict[str, object]])

Append new rows to the table.

Parameters:

rows (polars.DataFrame, pandas.DataFrame, pandas.Series, Dict[str, dict]) – The rows to append to the table. When using a DataFrame, the index indicates the row names. When using a polars DataFrame, an index column must be explicitly provided. When using a pandas Series, the index corresponds to the feature names and the Series name corresponds to the row name. When using a dict, the outer dict maps string row names to dictionaries that map string feature names to values. See examples below.

Examples

You can append new rows as a polars.DataFrame. Note that the index must be explicitly added.

>>> import polars as pl
>>> from lace.examples import Animals
>>> engine = Animals()
>>> crab_and_sponge = pl.DataFrame(
...     {
...         "index": ["crab", "sponge"],
...         "water": [1, 1],
...         "flippers": [0, 0],
...     }
... )
>>> engine.append_rows(crab_and_sponge)
>>> engine.index[-1]
'sponge'
>>> engine[-1, "water"]
1

You can append new rows as a pandas.DataFrame,

>>> import pandas as pd
>>> engine = Animals()
>>> crab_and_sponge = pd.DataFrame(
...     {
...         "index": ["crab", "sponge"],
...         "water": [1, 1],
...         "flippers": [0, 0],
...     }
... ).set_index("index")
>>> engine.append_rows(crab_and_sponge)
>>> engine.index[-1]
'sponge'
>>> engine[-1, "water"]
1

or a pandas.Series

>>> squid = pd.Series([0, 1], index=["water", "slow"], name="squid")
>>> engine.append_rows(squid)
>>> engine.index[-1]
'squid'
>>> engine[-1, "slow"]
1

or a dictionary of dictionaries

>>> engine = Animals()
>>> rows = {
...     "crab": {"water": 1, "flippers": 0},
...     "sponge": {"water": 1, "flippers": 0},
...     "squid": {"water": 1, "slow": 1},
... }
>>> engine.append_rows(rows)
>>> engine.index[-3:]
['crab', 'sponge', 'squid']
>>> engine[-3:, "flippers"]  
shape: (3, 2)
┌────────┬──────────┐
│ index  ┆ flippers │
│ ---    ┆ ---      │
│ str    ┆ u8       │
╞════════╪══════════╡
│ crab   ┆ 0        │
│ sponge ┆ 0        │
│ squid  ┆ null     │
└────────┴──────────┘