{"record":{"id":"3f2dc4d8855ff9e5","repo":"pandas-dev/pandas","slug":"default-empty-implementation-is-invalid-for-dtyp-3f2dc4","errorCode":null,"errorMessage":"Default 'empty' implementation is invalid for dtype='{dtype}'","messagePattern":"Default 'empty' implementation is invalid for dtype='(.+?)'","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/masked.py","lineNumber":205,"sourceCode":"        return result\n\n    @classmethod\n    def _empty(cls, shape: Shape, dtype: ExtensionDtype) -> Self:\n        \"\"\"\n        Create an ExtensionArray with the given shape and dtype.\n\n        See also\n        --------\n        ExtensionDtype.empty\n            ExtensionDtype.empty is the 'official' public version of this API.\n        \"\"\"\n        dtype = cast(\"BaseMaskedDtype\", dtype)\n        values: np.ndarray = np.empty(shape, dtype=dtype.type)\n        values.fill(dtype._internal_fill_value)\n        mask = np.ones(shape, dtype=bool)\n        result = cls(values, mask)\n        if not isinstance(result, cls) or dtype != result.dtype:\n            raise NotImplementedError(\n                f\"Default 'empty' implementation is invalid for dtype='{dtype}'\"\n            )\n        return result\n\n    def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:\n        # NEP 51: https://github.com/numpy/numpy/pull/22449\n        return str\n\n    @property\n    def dtype(self) -> BaseMaskedDtype:\n        raise AbstractMethodError(self)\n\n    @overload\n    def __getitem__(self, item: ScalarIndexer) -> Any: ...\n\n    @overload\n    def __getitem__(self, item: SequenceIndexer) -> Self: ...\n","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/masked.py#L187-L223","documentation":"Raised by BaseMaskedArray._empty when, after constructing an array filled with dtype._internal_fill_value and an all-True mask, the resulting object either is not an instance of cls or its dtype does not match the requested dtype. This indicates the masked-array subclass has misconfigured its class identity, its dtype property, or its _internal_fill_value, making the default empty() implementation invalid for that dtype.","triggerScenarios":"Authoring a custom ExtensionDtype/ExtensionArray subclass of BaseMaskedArray where dtype.type, _internal_fill_value, or the constructor don't line up; calling ExtensionDtype.empty(shape) on such a dtype.","commonSituations":"Library authors extending pandas masked arrays; mis-set class attributes after a refactor or dtype rename.","solutions":["Override _empty (or ExtensionDtype.empty) in the subclass to return a correctly-typed instance.","Verify dtype.type and dtype._internal_fill_value produce a value compatible with the subclass constructor.","Ensure cls(values, mask) returns an instance whose .dtype equals the requested dtype."],"exampleFix":"# before\nclass MyDtype(BaseMaskedDtype):\n    type = np.float64\n    _internal_fill_value = 0  # mismatched\n\n# after\nclass MyArray(BaseMaskedArray):\n    @classmethod\n    def _empty(cls, shape, dtype):\n        values = np.empty(shape, dtype=dtype.type)\n        values.fill(dtype._internal_fill_value)\n        return cls(values, np.ones(shape, dtype=bool))","handlingStrategy":"validation","validationCode":"def empty_works(dtype, shape):\n    arr = dtype.empty(shape)\n    return isinstance(arr, type(arr)) and arr.dtype == dtype","typeGuard":"def subclass_consistent(cls, dtype) -> bool:\n    try:\n        a = cls._empty((1,), dtype)\n        return isinstance(a, cls) and a.dtype == dtype\n    except Exception:\n        return False","tryCatchPattern":null,"preventionTips":["Override _empty/empty in custom BaseMaskedArray subclasses.","Keep dtype.type, _internal_fill_value, and the constructor in sync.","Add a unit test that calls ExtensionDtype.empty on every registered dtype."],"tags":["masked-array","subclassing","dtype"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}