pola-rs/polars · error
expected pandas DataFrame or Series, got
Error message
expected pandas DataFrame or Series, got {qualified_type_name(data)!r} What it means
from_pandas only accepts a pandas DataFrame, Series, Index or DatetimeIndex; any other object (including numpy arrays or plain sequences) falls through to the else branch and raises TypeError naming the actual received type.
Solutions
- Ensure the input is `pd.DataFrame` or `pd.Series`; convert if needed: `pd.DataFrame(obj)`.
- Use `pl.from_arrow` for pyarrow objects, `pl.from_dict` for dicts, `pl.from_records` for lists of records.
- Check `isinstance(data, (pd.DataFrame, pd.Series))` before calling.
Example fix
// before
pl.from_pandas(pa.table({'a': [1]}))
// after
pl.from_arrow(pa.table({'a': [1]})) Defensive patterns
Strategy: type-guard
Validate before calling
import pandas as pd
if not isinstance(data, (pd.DataFrame, pd.Series)):
raise TypeError(f'from_pandas requires pandas DataFrame/Series, got {type(data)}') Type guard
def is_pandas_frame(x) -> bool:
import pandas as pd
return isinstance(x, (pd.DataFrame, pd.Series)) Try / catch
try:
df = pl.from_pandas(data)
except TypeError:
df = pl.from_arrow(data) if hasattr(data, '__arrow_c_stream__') else pl.DataFrame(data) Prevention
- Keep converter choice explicit per input type
- Avoid passing polars/arrow objects back into from_pandas
- Add type asserts at module boundaries
When it happens
Trigger: Calling `pl.from_pandas(data)` with a non-pandas object such as a list, dict, pyarrow Table, numpy array, or None.
Common situations: Mixing up converters — passing Arrow or native Python containers to from_pandas; refactor of a pipeline changed the input type upstream; passing polars objects back in.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 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/a64f61e3b38e20ad.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/convert/general.py:735
"""
if include_index and isinstance(data, pd.Series):
data = data.reset_index()
if isinstance(data, (pd.Series, pd.Index, pd.DatetimeIndex)):
return wrap_s(pandas_to_pyseries("", data, nan_to_null=nan_to_null))
elif isinstance(data, pd.DataFrame):
return wrap_df(
pandas_to_pydf(
data,
schema_overrides=schema_overrides,
rechunk=rechunk,
nan_to_null=nan_to_null,
include_index=include_index,
)
)
else:
msg = f"expected pandas DataFrame or Series, got {qualified_type_name(data)!r}"
raise TypeError(msg)
@dataclass(frozen=True, slots=True)
class _TablePatterns:
"""Format-specific regex patterns for table parsing."""
cell_edge: re.Pattern[str]
cell_split: re.Pattern[str]
header_div: re.Pattern[str]
row_div: re.Pattern[str]
rstrip_chars: str
_TABLE_PATTERNS_CACHE: dict[TableRepr, _TablePatterns] = {}
def _build_table_patterns(table_repr: TableRepr) -> _TablePatterns:
if table_repr is TableRepr.UTF8:View on GitHub (pinned to fe841f959e)