{"record":{"id":"381c41f1bdb39e3d","repo":"pandas-dev/pandas","slug":"extensionarray-fillna-does-not-support-filling-wit-381c41","errorCode":null,"errorMessage":"ExtensionArray.fillna does not support filling with a dict. Use Series.fillna instead.","messagePattern":"ExtensionArray\\.fillna does not support filling with a dict\\. Use Series\\.fillna instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/array.py","lineNumber":858,"sourceCode":"\n        Returns\n        -------\n        SparseArray\n\n        Notes\n        -----\n        When `value` is specified, the result's ``fill_value`` depends on\n        ``self.fill_value``. The goal is to maintain low-memory use.\n\n        If ``self.fill_value`` is NA, the result dtype will be\n        ``SparseDtype(self.dtype, fill_value=value)``. This will preserve\n        amount of memory used before and after filling.\n\n        When ``self.fill_value`` is not NA, the result dtype will be\n        ``self.dtype``. Again, this preserves the amount of memory used.\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        if limit is not None:\n            raise ValueError(\"limit must be None\")\n        new_values = np.where(isna(self.sp_values), value, self.sp_values)\n\n        if self._null_fill_value:\n            # This is essentially just updating the dtype.\n            new_dtype = SparseDtype(self.dtype.subtype, fill_value=value)\n        else:\n            new_dtype = self.dtype\n\n        return self._simple_new(new_values, self._sparse_index, new_dtype)\n\n    def shift(self, periods: int = 1, fill_value=None) -> Self:\n        if not len(self) or periods == 0:\n            return self.copy()","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/sparse/array.py#L840-L876","documentation":"Raised by SparseArray.fillna when `value` is a dict. The base ExtensionArray.fillna accepts dicts (position->value), but SparseArray only supports a scalar fill because each NA in sp_values maps to the same stored fill value. The message points users to Series.fillna for dict semantics.","triggerScenarios":"sparse_arr.fillna({0: 5, 2: 7}); passing a dict of position-keyed fills to the underlying .array via Series.fillna internals that forward the dict to the EA.","commonSituations":"Reusing a dict fillna pattern from dense Series code on the .array directly; generic fillna wrappers that pass dicts through.","solutions":["Use Series.fillna with a dict: pd.Series(sparse_arr).fillna({0:5, 2:7}).","If only a single value is needed, pass a scalar: sparse_arr.fillna(0).","For positional fills, rebuild from dense np.where logic."],"exampleFix":"// before\narr.fillna({0: 5, 2: 7})\n// after\npd.Series(arr).fillna({0: 5, 2: 7}).array","handlingStrategy":"fallback","validationCode":"import pandas as pd\n\ndef fillna_sparse(arr, value):\n    if isinstance(value, dict):\n        return pd.Series(arr).fillna(value).array\n    return arr.fillna(value)","typeGuard":"def is_scalar_fill(value) -> bool:\n    from pandas.api.types import is_scalar\n    return is_scalar(value)","tryCatchPattern":"try:\n    return arr.fillna(value)\nexcept TypeError as e:\n    if 'dict' in str(e):\n        return pd.Series(arr).fillna(value).array\n    raise","preventionTips":["Use Series.fillna for dict-based fills.","Pass only scalars to SparseArray.fillna.","Wrap sparse arrays in Series for rich fillna semantics."],"tags":["pandas","sparse","sparse-array","fillna","dict"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}