pola-rs/polars · error
expected data of type Sequence, got
Error message
expected data of type Sequence, got {type(data).__name__!r}\n\nHint: Try passing your data to the DataFrame constructor instead, e.g. `pl.DataFrame(data)`. What it means
pl.from_records requires data to be an instance of collections.abc.Sequence (list, tuple, etc.). Passing a non-sequence such as a dict, set, generator, or numpy array that doesn't qualify raises a TypeError with a hint to use the DataFrame constructor instead.
Solutions
- Use pl.DataFrame(data) or pl.from_dict(data) for dict-of-columns input.
- Convert to a list first: pl.from_records(list(data)).
- For generators: pl.from_records(list(gen)) or pl.DataFrame(gen).
- For numpy arrays, use pl.DataFrame(arr) or pass arr.tolist().
Example fix
// before
pl.from_records({"a": [1, 2], "b": [3, 4]})
// after
pl.from_dict({"a": [1, 2], "b": [3, 4]}) Defensive patterns
Strategy: type-guard
Validate before calling
from collections.abc import Sequence
if not isinstance(data, Sequence):
raise TypeError("from_records needs a Sequence; use pl.DataFrame/pl.from_dict for other shapes")
pl.from_records(data) Type guard
from collections.abc import Sequence, Mapping
def is_record_sequence(v) -> bool:
return isinstance(v, Sequence) and not isinstance(v, (str, bytes, Mapping)) Try / catch
try:
df = pl.from_records(data)
except TypeError:
df = pl.DataFrame(data) # handles dicts, arrays, generators Prevention
- Use from_dict for column-mapping dicts and from_records for row sequences.
- Materialize generators/sets with list() before calling.
- When unsure, pl.DataFrame(data) accepts the widest range of inputs.
When it happens
Trigger: pl.from_records({'a': [1,2]}), pl.from_records(some_set), pl.from_records(some_generator), or passing a dict-of-columns that should go through pl.DataFrame.
Common situations: Confusing from_records (rows as sequences) with from_dict (columns by name); passing generators or sets that were assumed to be 'record-like'; numpy arrays routed to the wrong constructor.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- arr.to_struct() got a str instead of a list. hint: pass
- cannot call `map_groups` when filtering groups with `having`
- cannot call `map_groups` when grouping by an expression
- cannot call `map_groups` when grouping by named expressions
- cannot set Series of dtype
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/e16133b3af8bd1fd.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/convert/general.py:297
>>> df
shape: (3, 2)
┌─────┬─────┐
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1 ┆ 4 │
│ 2 ┆ 5 │
│ 3 ┆ 6 │
└─────┴─────┘
"""
if not isinstance(data, Sequence):
msg = (
f"expected data of type Sequence, got {type(data).__name__!r}"
"\n\nHint: Try passing your data to the DataFrame constructor instead,"
" e.g. `pl.DataFrame(data)`."
)
raise TypeError(msg)
return wrap_df(
sequence_to_pydf(
data,
schema=schema,
schema_overrides=schema_overrides,
strict=strict,
orient=orient,
infer_schema_length=infer_schema_length,
)
)
def from_numpy(
data: np.ndarray[Any, Any],
schema: SchemaDefinition | None = None,
*,
schema_overrides: SchemaDict | None = None,View on GitHub (pinned to fe841f959e)