pandas-dev/pandas · error · ValueError

Length of 'value' does not match. Got ({len(value)}) expect

Error message

Length of 'value' does not match. Got ({len(value)})  expected {len(self)}

What it means

Raised by ArrowExtensionArray.fillna when the array-like fill value is a different length than the target array. For position-wise filling (no `limit`), pandas requires the value array to be broadcastable 1:1 against the existing array, so a length mismatch is a hard error rather than an alignment attempt.

Source

Thrown at pandas/core/arrays/arrow/array.py:1706

        Length: 6, dtype: int64[pyarrow]
        """
        if not self._hasna:
            return self.copy()

        if isinstance(value, dict):
            raise TypeError(
                "ExtensionArray.fillna does not support filling with a dict. "
                "Use Series.fillna instead."
            )

        if limit is not None:
            return super().fillna(value=value, limit=limit, copy=copy)

        if isinstance(value, (np.ndarray, ExtensionArray)):
            # Similar to check_value_size, but we do not mask here since we may
            #  end up passing it to the super() method.
            if len(value) != len(self):
                raise ValueError(
                    f"Length of 'value' does not match. Got ({len(value)}) "
                    f" expected {len(self)}"
                )

        try:
            fill_value = self._box_pa(value, pa_type=self._pa_array.type)
        except pa.ArrowTypeError as err:
            msg = f"Invalid value '{value!s}' for dtype '{self.dtype}'"
            raise TypeError(msg) from err

        try:
            return self._from_pyarrow_array(
                _safe_fill_null(self._pa_array, fill_value=fill_value)
            )
        except pa.ArrowNotImplementedError:
            # ArrowNotImplementedError: Function 'coalesce' has no kernel
            #   matching input types (duration[ns], duration[ns])
            # TODO: remove try/except wrapper if/when pyarrow implements

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Reindex the value array to the same length/positions as the target: `value = value.reindex_like(target)` or slice to `len(target)`.
  2. Pass a scalar fill value when you want constant filling instead of per-position values.
  3. If positional alignment is intended, drop NAs from the value first or use `Series.align` before fillna.

Example fix

// before
s = pd.Series([1, None, 3], dtype="int64[pyarrow]")
s.fillna(pd.array([0, 0], dtype="int64[pyarrow]"))

// after
s.fillna(pd.array([0, 0, 0], dtype="int64[pyarrow]"))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
from pandas.api.extensions import ExtensionArray

def check_fillna_value(arr, value):
    if isinstance(value, (np.ndarray, ExtensionArray)) and len(value) != len(arr):
        raise ValueError(f"value length {len(value)} != array length {len(arr)}")
    return value

Type guard

def is_aligned_fill_value(arr, value) -> bool:
    import numpy as np
    from pandas.api.extensions import ExtensionArray
    return (
        not isinstance(value, (np.ndarray, ExtensionArray))
        or len(value) == len(arr)
    )

Try / catch

try:
    arr.fillna(value)
except ValueError as e:
    if "Length of 'value' does not match" in str(e):
        arr.fillna(value[: len(arr)])  # or align properly
    else:
        raise

Prevention

When it happens

Trigger: Calling `arr.fillna(other_array)` or `series.fillna(other_array)` on a pyarrow-backed ExtensionArray where `other_array` is an np.ndarray or ExtensionArray whose `len()` differs from `len(arr)`, and `limit` is None.

Common situations: Filling NAs from another column/Series whose index is misaligned, reusing a fill array computed on a filtered/droppedna frame, or passing a Python list where the caller expected element-wise alignment.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/aad830669f13f495. Report an issue: GitHub.