pola-rs/polars · error
expected object supporting the PyCapsule Interface, got
Error message
expected object supporting the PyCapsule Interface, got {qualified_type_name(df)!r} What it means
`pl.from_dataframe` implements the Arrow PyCapsule Interface: it accepts any object exposing `__arrow_c_stream__`/`__arrow_c_array__`. This TypeError is raised when the object does not implement that interface.
Solutions
- Pass an object implementing the PyCapsule Interface: pyarrow Table/RecordBatch (recent versions), polars DataFrame, pandas >= 2 with `pd.options.future.infer_string`/modern builds.
- For pandas input use `pl.from_pandas(df)` or `pl.from_arrow(pa.Table.from_pandas(df))`.
- Check `polars.convert.is_pycapsule(obj)` before calling and pick an alternate converter otherwise.
Example fix
// before pl.from_dataframe(pandas_df) # old pandas, no capsule // after pl.from_pandas(pandas_df)
Defensive patterns
Strategy: type-guard
Validate before calling
from polars.convert import is_pycapsule
if not is_pycapsule(df):
df = pa.Table.from_pandas(df) if isinstance(df, pd.DataFrame) else pa.table(df) Type guard
def supports_pycapsule(x) -> bool:
return hasattr(x, '__arrow_c_stream__') or hasattr(x, '__arrow_c_array__') Try / catch
try:
out = pl.from_dataframe(obj)
except TypeError:
out = pl.from_arrow(pa.table(obj)) if hasattr(obj, 'keys') else pl.from_pandas(obj) Prevention
- Require pandas >= 2 for PyCapsule-based interop
- Check is_pycapsule/hasattr __arrow_c_stream__ before dispatch
- Prefer pyarrow Tables as the interchange format
When it happens
Trigger: Calling `pl.from_dataframe(df)` with a pandas DataFrame (older pandas without PyCapsule support), a pyarrow Table used where a capsule-backed object is required, a dict, or any plain Python object.
Common situations: pandas versions below 2.x (no `__arrow_c_stream__`); relying on from_dataframe for pandas data that has since changed type; passing dictionaries expecting automatic inference.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 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 parse Python data type
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/1b373c65fdfca38b.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/convert/general.py:1163
--------
Convert a pandas dataframe to Polars.
>>> import pandas as pd
>>> df_pd = pd.DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["x", "y"]})
>>> pl.from_dataframe(df_pd)
shape: (2, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ i64 ┆ f64 ┆ str │
╞═════╪═════╪═════╡
│ 1 ┆ 3.0 ┆ x │
│ 2 ┆ 4.0 ┆ y │
└─────┴─────┴─────┘
"""
if not is_pycapsule(df):
msg = f"expected object supporting the PyCapsule Interface, got {qualified_type_name(df)!r}"
raise TypeError(msg)
return pycapsule_to_frame(df, rechunk=rechunk)
View on GitHub (pinned to fe841f959e)