Skip to content

data

relationalai.semantics
data(
data: DataFrame | list[tuple] | list[dict], columns: list[str] | None = None
) -> Data

Create a temporary table from in-memory Python or pandas data.

This is a convenience wrapper around Model.data that operates on the single active model. The returned Data object acts like a table: you can refer to its columns (e.g., d.a, d["a"]) in select, where, and define. You can also convert it to a schema via Table.to_schema and use that schema when creating new entities.

If you have more than one model, call Model.data on the specific model instead.

  • data

    (pandas.DataFrame | list[tuple] | list[dict]) - The input data. Supported forms are:

    • A pandas DataFrame
    • A list of tuples (each tuple is a row)
    • A list of dicts (each dict is a row)
  • columns

    (list[str], default: None) - Column names to use when data is a list of tuples. If omitted, the data will use default integer column labels 0, 1, 2, … . Those default labels are exposed as col0, col1, col2, … so you can write d.col0, d.col1, etc.

  • Data - A table-like object backed by the provided data.
  • relationalai.util.error.RAIException - If there is no active model, or if multiple models exist and the active model would be ambiguous.
  • TypeError - If data cannot be converted into a pandas DataFrame.

Create data from a list of dicts and use it directly:

>>> from relationalai.semantics import Model, data, define, select
>>> m = Model()
>>> people_rows = data([
... {"name": "Alice", "age": 10},
... {"name": "Bob", "age": 30},
... {"name": "Charlie", "age": 70},
... ])
>>> select(people_rows.name, people_rows.age).to_df()

Use data to create entities by converting to a schema using Table.to_schema and passing that schema to Concept.new:

>>> Person = m.Concept("Person")
>>> define(Person.new(people_rows.to_schema()))
>>> select(Person.name, Person.age).to_df()

Provide column names when loading tuple rows:

>>> measurements = data([(0, 72.5), (1, 71.9)], columns=["minute", "temperature"])
>>> select(measurements.minute, measurements.temperature).to_df()
RelationalAI Documentation
└──  Build With RelationalAI
    └──  Understand how PyRel works > Build a semantic model
        └──  Declare data sources
            └──  Use inline Python data with Model.data