Skip to content

[Bug]: constructing a Categorical column from Python values raises on the PyArrow backend #3775

Description

@mkzung

Tracking

Describe the bug

On the PyArrow backend, building a Categorical column straight from Python values raises. The same calls work on pandas and Polars, and casting an existing PyArrow column to Categorical already works, so it looks like only the construct-from-Python-values path is affected.

Steps or code to reproduce the bug

import narwhals as nw

# ArrowTypeError: Array chunks must all be same type
nw.new_series("s", ["a", "b", "a"], dtype=nw.Categorical, backend="pyarrow")

# ArrowInvalid: Column data for field 0 with type dictionary<values=string, indices=int32> ...
nw.from_dicts([{"a": "x"}, {"a": "y"}], schema={"a": nw.Categorical()}, backend="pyarrow")

For contrast, these all work:

nw.new_series("s", ["a", "b"], dtype=nw.Categorical, backend="pandas")               # ok
nw.new_series("s", ["a", "b"], dtype=nw.Categorical, backend="polars")               # ok
nw.from_dict({"a": ["x", "y"]}, schema={"a": nw.Categorical()}, backend="pyarrow")   # ok
nw.from_dict({"a": ["x", "y"]}, backend="pyarrow")["a"].cast(nw.Categorical)          # ok

Expected results

The Categorical column is built on PyArrow too, the same as on pandas and Polars.

Actual results

new_series raises ArrowTypeError: Array chunks must all be same type, and from_dicts raises ArrowInvalid: Column data for field 0 with type dictionary<...>. Categorical maps to pa.dictionary(uint32, string) and isn't in UNSUPPORTED_DTYPES; the arrow construction path (pa.chunked_array / pa.Table.from_pylist) can't build a dictionary-typed array straight from Python values.

This came up in #3757.

narwhals.show_versions()

Python dependencies:
     narwhals: 2.23.0
        numpy: 2.2.6
       pandas: 2.3.3
      pyarrow: 23.0.1
       polars: 1.42.1
         dask: 2026.7.0
       duckdb: 1.5.4
         ibis: 12.0.0
     sqlframe: 4.3.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions