Skip to content

data

relationalai.semantics
data(
data: DataFrame | list[tuple] | list[dict],
columns: list[str] | None = None,
schema: dict[str, Concept] | 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.

  • schema

    (dict[str, Concept], default: None) - Explicit column type overrides. Keys are column names and values are Concept types (for example Integer or String). When provided, the type for each named column is taken from this mapping instead of being inferred from the data. Columns not present in the mapping are still inferred from the data as usual.

  • 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
└──  Release Notes
    └──  Python API Release Notes
        ├──  What’s New in Version 1.0.5
        │   └──  New Features and Enhancements
        └──  What’s New in Version 1.0.6
            └──  Bug Fixes