Skip to content

Commit e49387d

Browse files
authored
PERF: BaseMaskedArray._empty (#53040)
* PERF: BaseMaskedArray._empty * update * use .fill method
1 parent aee55c9 commit e49387d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/arrays/masked.py

+13
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False) -> Self:
131131
values, mask = cls._coerce_to_array(scalars, dtype=dtype, copy=copy)
132132
return cls(values, mask)
133133

134+
@classmethod
135+
@doc(ExtensionArray._empty)
136+
def _empty(cls, shape: Shape, dtype: ExtensionDtype):
137+
values = np.empty(shape, dtype=dtype.type)
138+
values.fill(cls._internal_fill_value)
139+
mask = np.ones(shape, dtype=bool)
140+
result = cls(values, mask)
141+
if not isinstance(result, cls) or dtype != result.dtype:
142+
raise NotImplementedError(
143+
f"Default 'empty' implementation is invalid for dtype='{dtype}'"
144+
)
145+
return result
146+
134147
@property
135148
def dtype(self) -> BaseMaskedDtype:
136149
raise AbstractMethodError(self)

pandas/core/dtypes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def construct_array_type(cls) -> type_t[ExtensionArray]:
211211
"""
212212
raise AbstractMethodError(cls)
213213

214-
def empty(self, shape: Shape) -> type_t[ExtensionArray]:
214+
def empty(self, shape: Shape) -> ExtensionArray:
215215
"""
216216
Construct an ExtensionArray of this dtype with the given shape.
217217

0 commit comments

Comments
 (0)