{"record":{"id":"1b373c65fdfca38b","repo":"pola-rs/polars","slug":"expected-object-supporting-the-pycapsule-interface","errorCode":null,"errorMessage":"expected object supporting the PyCapsule Interface, got {qualified_type_name(df)!r}","messagePattern":"expected object supporting the PyCapsule Interface, got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/convert/general.py","lineNumber":1163,"sourceCode":"    --------\n    Convert a pandas dataframe to Polars.\n\n    >>> import pandas as pd\n    >>> df_pd = pd.DataFrame({\"a\": [1, 2], \"b\": [3.0, 4.0], \"c\": [\"x\", \"y\"]})\n    >>> pl.from_dataframe(df_pd)\n    shape: (2, 3)\n    ┌─────┬─────┬─────┐\n    │ a   ┆ b   ┆ c   │\n    │ --- ┆ --- ┆ --- │\n    │ i64 ┆ f64 ┆ str │\n    ╞═════╪═════╪═════╡\n    │ 1   ┆ 3.0 ┆ x   │\n    │ 2   ┆ 4.0 ┆ y   │\n    └─────┴─────┴─────┘\n    \"\"\"\n    if not is_pycapsule(df):\n        msg = f\"expected object supporting the PyCapsule Interface, got {qualified_type_name(df)!r}\"\n        raise TypeError(msg)\n\n    return pycapsule_to_frame(df, rechunk=rechunk)\n","sourceCodeStart":1145,"sourceCodeEnd":1166,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/convert/general.py#L1145-L1166","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npl.from_dataframe(pandas_df)  # old pandas, no capsule\n// after\npl.from_pandas(pandas_df)","handlingStrategy":"type-guard","validationCode":"from polars.convert import is_pycapsule\nif not is_pycapsule(df):\n    df = pa.Table.from_pandas(df) if isinstance(df, pd.DataFrame) else pa.table(df)","typeGuard":"def supports_pycapsule(x) -> bool:\n    return hasattr(x, '__arrow_c_stream__') or hasattr(x, '__arrow_c_array__')","tryCatchPattern":"try:\n    out = pl.from_dataframe(obj)\nexcept TypeError:\n    out = pl.from_arrow(pa.table(obj)) if hasattr(obj, 'keys') else pl.from_pandas(obj)","preventionTips":["Require pandas >= 2 for PyCapsule-based interop","Check is_pycapsule/hasattr __arrow_c_stream__ before dispatch","Prefer pyarrow Tables as the interchange format"],"tags":["python","polars","pycapsule","arrow","type-error"],"backgroundTag":"incompatible-source-type","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}