pola-rs/polars · error · ValueError

cannot parse numpy data type

Error message

cannot parse numpy data type {dtype!r} into Polars data type

What it means

numpy_char_code_to_dtype maps a numpy dtype (kind, itemsize) pair to a Polars dtype via NUMPY_KIND_AND_ITEMSIZE_TO_DTYPE; unsupported combinations raise ValueError. It handles the set of numpy dtypes polars can represent losslessly.

Solutions

  1. Cast the array first: arr.astype(np.float64) (or int64/uint64/datetime64[us]).
  2. For float16: arr.astype(np.float32).
  3. For strings/objects: convert to Python lists or let polars infer (pl.Series(arr.tolist())).

Example fix

// before
pl.Series(np.array([1.0, 2.0], dtype=np.float16))
// after
arr = np.array([1.0, 2.0], dtype=np.float16).astype(np.float32)
pl.Series(arr)
Defensive patterns

Strategy: validation

Validate before calling

OK_KINDS = {'b', 'i', 'u', 'f', 'm', 'M'}
def polars_compatible(arr: np.ndarray) -> bool:
    k, itemsize = arr.dtype.kind, arr.dtype.itemsize
    if k in 'iu' and itemsize <= 8: return True
    if k == 'f' and itemsize in (4, 8): return True
    if k == 'b': return True
    if k in 'mM': return True
    return False
# if not polars_compatible(arr): arr = arr.astype(np.float64 or np.int64)

Try / catch

try:
    s = pl.Series(arr)
except ValueError as e:
    if "cannot parse numpy data type" in str(e):
        s = pl.Series(arr.tolist())
    else:
        raise

Prevention

When it happens

Trigger: Passing numpy arrays of kind 'U'/'S' with odd itemsizes, longdouble ('g'), complex ('c'), float16 ('f' itemsize 2), or >64-bit ints into pl.Series/from_numpy; __array_ufunc__ paths receiving such arrays.

Common situations: float16 arrays from ML preprocessing; complex arrays from signal processing; object arrays with untyped numpy dtype('O'); big-endian ('>i8') dtype variants on some versions.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/d68e29b50fedbd7c. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/datatypes/convert.py:339

        dtype.kind,
        dtype.itemsize,
    ) in DataTypeMappings.NUMPY_KIND_AND_ITEMSIZE_TO_DTYPE


def numpy_char_code_to_dtype(dtype_char: str) -> PolarsDataType:
    """Convert a numpy character dtype to a Polars dtype."""
    dtype = np.dtype(dtype_char)
    if dtype.kind == "U":
        return String
    elif dtype.kind == "S":
        return Binary
    try:
        return DataTypeMappings.NUMPY_KIND_AND_ITEMSIZE_TO_DTYPE[
            dtype.kind, dtype.itemsize
        ]
    except KeyError:  # pragma: no cover
        msg = f"cannot parse numpy data type {dtype!r} into Polars data type"
        raise ValueError(msg) from None


def maybe_cast(el: Any, dtype: PolarsDataType) -> Any:
    """Try casting a value to a value that is valid for the given Polars dtype."""
    # cast el if it doesn't match
    from polars._utils.convert import (
        datetime_to_int,
        timedelta_to_int,
    )

    time_unit: TimeUnit
    if isinstance(el, datetime):
        time_unit = getattr(dtype, "time_unit", "us")
        return datetime_to_int(el, time_unit)
    elif isinstance(el, timedelta):
        time_unit = getattr(dtype, "time_unit", "us")
        return timedelta_to_int(el, time_unit)

View on GitHub (pinned to fe841f959e)