pola-rs/polars · error · TypeError
cannot convert List dtype to Tensor (use Array dtype instead
Error message
cannot convert List dtype to Tensor (use Array dtype instead)
What it means
to_torch conversion guard: PyTorch tensors need fixed-shape elements, so a List-dtype Series cannot be converted; the guard directs users to the fixed-width Array dtype. Raised before any tensor construction.
Source
Thrown at py-polars/src/polars/series/series.py:4985
torch = import_optional("torch")
# PyTorch tensors do not support uint16/32/64
if self.dtype in (UInt32, UInt64):
srs = self.cast(Int64)
elif self.dtype == UInt16:
srs = self.cast(Int32)
else:
srs = self
# we have to build the tensor from a writable array or PyTorch will complain
# about it (writing to a readonly array results in undefined behavior)
numpy_array = srs.to_numpy(writable=True)
try:
tensor = torch.from_numpy(numpy_array)
except TypeError:
if self.dtype == List:
msg = "cannot convert List dtype to Tensor (use Array dtype instead)"
raise TypeError(msg) from None
raise
# note: named tensors are currently experimental
# tensor.rename(self.name)
return tensor
@removed_parameters(
RenamedParameter(
name="future",
new_name="compat_level",
deprecated_in="1.1",
removed_in="2.0",
),
)
def to_arrow(self, *, compat_level: CompatLevel | None = None) -> pa.Array:
"""
Return the underlying Arrow array.
If the Series contains only a single chunk this operation is zero copy.View on GitHub (pinned to 68506541d2)
Solutions
- Convert the column to the fixed-width Array dtype (e.g. `cast(pl.Array(inner, size))`) before converting to a tensor.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at py-polars/src/polars/series/series.py:5145 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19).
Data as JSON: /api/errors/0d284de271bddf1b.
Report an issue: GitHub.