{"record":{"id":"aad830669f13f495","repo":"pandas-dev/pandas","slug":"length-of-value-does-not-match-got-len-value","errorCode":null,"errorMessage":"Length of 'value' does not match. Got ({len(value)})  expected {len(self)}","messagePattern":"Length of 'value' does not match\\. Got \\((.+?)\\)  expected (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":1706,"sourceCode":"        Length: 6, dtype: int64[pyarrow]\n        \"\"\"\n        if not self._hasna:\n            return self.copy()\n\n        if isinstance(value, dict):\n            raise TypeError(\n                \"ExtensionArray.fillna does not support filling with a dict. \"\n                \"Use Series.fillna instead.\"\n            )\n\n        if limit is not None:\n            return super().fillna(value=value, limit=limit, copy=copy)\n\n        if isinstance(value, (np.ndarray, ExtensionArray)):\n            # Similar to check_value_size, but we do not mask here since we may\n            #  end up passing it to the super() method.\n            if len(value) != len(self):\n                raise ValueError(\n                    f\"Length of 'value' does not match. Got ({len(value)}) \"\n                    f\" expected {len(self)}\"\n                )\n\n        try:\n            fill_value = self._box_pa(value, pa_type=self._pa_array.type)\n        except pa.ArrowTypeError as err:\n            msg = f\"Invalid value '{value!s}' for dtype '{self.dtype}'\"\n            raise TypeError(msg) from err\n\n        try:\n            return self._from_pyarrow_array(\n                _safe_fill_null(self._pa_array, fill_value=fill_value)\n            )\n        except pa.ArrowNotImplementedError:\n            # ArrowNotImplementedError: Function 'coalesce' has no kernel\n            #   matching input types (duration[ns], duration[ns])\n            # TODO: remove try/except wrapper if/when pyarrow implements","sourceCodeStart":1688,"sourceCodeEnd":1724,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L1688-L1724","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reindex the value array to the same length/positions as the target: `value = value.reindex_like(target)` or slice to `len(target)`.","Pass a scalar fill value when you want constant filling instead of per-position values.","If positional alignment is intended, drop NAs from the value first or use `Series.align` before fillna."],"exampleFix":"// before\ns = pd.Series([1, None, 3], dtype=\"int64[pyarrow]\")\ns.fillna(pd.array([0, 0], dtype=\"int64[pyarrow]\"))\n\n// after\ns.fillna(pd.array([0, 0, 0], dtype=\"int64[pyarrow]\"))","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom pandas.api.extensions import ExtensionArray\n\ndef check_fillna_value(arr, value):\n    if isinstance(value, (np.ndarray, ExtensionArray)) and len(value) != len(arr):\n        raise ValueError(f\"value length {len(value)} != array length {len(arr)}\")\n    return value","typeGuard":"def is_aligned_fill_value(arr, value) -> bool:\n    import numpy as np\n    from pandas.api.extensions import ExtensionArray\n    return (\n        not isinstance(value, (np.ndarray, ExtensionArray))\n        or len(value) == len(arr)\n    )","tryCatchPattern":"try:\n    arr.fillna(value)\nexcept ValueError as e:\n    if \"Length of 'value' does not match\" in str(e):\n        arr.fillna(value[: len(arr)])  # or align properly\n    else:\n        raise","preventionTips":["Always align value arrays with the target index before fillna (Series.align or reindex_like).","Prefer scalar fill values unless you intentionally need per-position fills.","Add a length assertion in test code for fill-value sources computed from filtered data."],"tags":["arrow","fillna","length-mismatch","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}