pola-rs/polars · error · NoDataError

no data, cannot infer schema

Error message

no data, cannot infer schema

What it means

pl.from_dicts raises NoDataError when the input list is empty AND neither a schema nor schema_overrides is provided, because column names and types cannot be inferred from zero rows. Supplying a schema removes the ambiguity and makes empty input valid.

Solutions

  1. Provide a schema: pl.from_dicts([], schema={'a': pl.Int64, 'b': pl.Utf8}).
  2. Provide schema_overrides for the expected columns.
  3. Guard: return pl.DataFrame(schema=...) or None when the input list is empty.
  4. Catch polars.NoDataError and substitute an empty typed DataFrame.

Example fix

// before
 df = pl.from_dicts(records)  # records == []
// after
 df = pl.from_dicts(records, schema={"id": pl.Int64, "name": pl.String})
Defensive patterns

Strategy: try-catch

Validate before calling

if not data and not (schema or schema_overrides):
    # supply an empty typed frame instead
    df = pl.DataFrame(schema=schema or {"a": pl.String})
else:
    df = pl.from_dicts(data, schema=schema, schema_overrides=schema_overrides)

Type guard

def has_schema_kwargs(schema, schema_overrides) -> bool:
    return schema is not None or schema_overrides is not None

Try / catch

from polars.exceptions import NoDataError
try:
    df = pl.from_dicts(records)
except NoDataError:
    df = pl.DataFrame(schema=expected_schema)

Prevention

When it happens

Trigger: pl.from_dicts([]) with no schema argument; also falsy inputs like None or empty sequences with schema=None and schema_overrides=None.

Common situations: Building a DataFrame from API/DB results that returned no rows; aggregating per-partition dicts where every partition happened to be empty.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/e96dccc1e905b695. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/convert/general.py:216

    >>> pl.from_dicts(
    ...     data,
    ...     schema=["a", "b", "c", "d"],
    ...     schema_overrides={"c": pl.Float64, "d": pl.String},
    ... )
    shape: (3, 4)
    ┌─────┬─────┬──────┬──────┐
    │ a   ┆ b   ┆ c    ┆ d    │
    │ --- ┆ --- ┆ ---  ┆ ---  │
    │ i64 ┆ i64 ┆ f64  ┆ str  │
    ╞═════╪═════╪══════╪══════╡
    │ 1   ┆ 4   ┆ null ┆ null │
    │ 2   ┆ 5   ┆ null ┆ null │
    │ 3   ┆ 6   ┆ null ┆ null │
    └─────┴─────┴──────┴──────┘
    """
    if not data and not (schema or schema_overrides):
        msg = "no data, cannot infer schema"
        raise NoDataError(msg)

    return pl.DataFrame(
        data,
        schema=schema,
        schema_overrides=schema_overrides,
        strict=strict,
        infer_schema_length=infer_schema_length,
    )


def from_records(
    data: Sequence[Any],
    schema: SchemaDefinition | None = None,
    *,
    schema_overrides: SchemaDict | None = None,
    strict: bool = True,
    orient: Orientation | None = None,
    infer_schema_length: int | None = N_INFER_DEFAULT,

View on GitHub (pinned to fe841f959e)