{"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":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":1731,"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":1713,"sourceCodeEnd":1749,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/arrow/array.py#L1713-L1749","documentation":"Raised by ArrowExtensionArray.fillna when `value` is an ndarray or ExtensionArray whose length differs from len(self). Array-like fill values must align positionally with the target array, so a length mismatch is rejected before attempting to box the value into pyarrow.","triggerScenarios":"arr.fillna(other_array) where len(other_array) != len(arr); passing a column from a differently-shaped DataFrame as the fill value; using a boolean mask of the wrong length; chaining operations that produced an array of unexpected size.","commonSituations":"Filling NA from a parallel column whose index was not aligned; off-by-one in computing the fill array; mixing arrays from different groupby groups; broadcasting assumptions that do not hold for fillna.","solutions":["Align lengths explicitly: ensure len(value) == len(arr) before calling fillna.","Use a Series and let pandas align on the index: pd.Series(arr).fillna(pd.Series(value)).","Broadcast a scalar instead of an array when the fill is uniform.","Reindex the fill array to match before passing: value = value[:len(arr)] or use np.broadcast_to."],"exampleFix":"# before\narr = pd.array([1, None, 3, None], dtype=\"int64[pyarrow]\")\nfill = pd.array([0, 0, 0], dtype=\"int64[pyarrow]\")\narr.fillna(fill)  # raises ValueError\n\n# after\nfill = pd.array([0, 0, 0, 0], dtype=\"int64[pyarrow]\")\narr.fillna(fill)","handlingStrategy":"validation","validationCode":"def safe_fillna_array(arr, value):\n    if hasattr(value, '__len__') and not isinstance(value, str):\n        if len(value) != len(arr):\n            raise ValueError(f'len(value)={len(value)} != len(arr)={len(arr)}')\n    return arr.fillna(value)","typeGuard":"def length_matches(value, target_len: int) -> bool:\n    return not hasattr(value, '__len__') or len(value) == target_len","tryCatchPattern":null,"preventionTips":["Validate len(value) == len(arr) before array-level fillna.","Use Series.fillna to leverage automatic index alignment.","Prefer scalar fill values for uniform replacements."],"tags":["fillna","length-mismatch","validation","alignment"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}