85
85
AstypeArg ,
86
86
AxisInt ,
87
87
Dtype ,
88
+ DtypeObj ,
88
89
FillnaOptions ,
89
90
InterpolateOptions ,
90
91
NumpySorter ,
@@ -262,14 +263,7 @@ class ExtensionArray:
262
263
# ------------------------------------------------------------------------
263
264
264
265
@classmethod
265
- def _from_sequence (
266
- cls ,
267
- scalars ,
268
- * ,
269
- dtype : Dtype | None = None ,
270
- fill_value : object = None ,
271
- copy : bool = False ,
272
- ):
266
+ def _from_sequence (cls , scalars , * , dtype : Dtype | None = None , copy : bool = False ):
273
267
"""
274
268
Construct a new ExtensionArray from a sequence of scalars.
275
269
@@ -297,6 +291,35 @@ def _from_sequence(
297
291
"""
298
292
raise AbstractMethodError (cls )
299
293
294
+ @classmethod
295
+ def _from_scalars (cls , scalars , * , dtype : DtypeObj ) -> Self :
296
+ """
297
+ Strict analogue to _from_sequence, allowing only sequences of scalars
298
+ that should be specifically inferred to the given dtype.
299
+ Parameters
300
+ ----------
301
+ scalars : sequence
302
+ dtype : ExtensionDtype
303
+ Raises
304
+ ------
305
+ TypeError or ValueError
306
+ Notes
307
+ -----
308
+ This is called in a try/except block when casting the result of a
309
+ pointwise operation.
310
+ """
311
+ try :
312
+ return cls ._from_sequence (scalars , dtype = dtype , copy = False )
313
+ except (ValueError , TypeError ):
314
+ raise
315
+ except Exception :
316
+ warnings .warn (
317
+ "_from_scalars should only raise ValueError or TypeError. "
318
+ "Consider overriding _from_scalars where appropriate." ,
319
+ stacklevel = find_stack_level (),
320
+ )
321
+ raise
322
+
300
323
@classmethod
301
324
def _from_sequence_of_strings (
302
325
cls , strings , * , dtype : Dtype | None = None , copy : bool = False
@@ -2092,7 +2115,7 @@ def _rank(
2092
2115
)
2093
2116
2094
2117
@classmethod
2095
- def _empty (cls , shape : Shape , dtype : ExtensionDtype , fill_value : object = None ):
2118
+ def _empty (cls , shape : Shape , dtype : ExtensionDtype ):
2096
2119
"""
2097
2120
Create an ExtensionArray with the given shape and dtype.
2098
2121
@@ -2104,7 +2127,7 @@ def _empty(cls, shape: Shape, dtype: ExtensionDtype, fill_value: object = None):
2104
2127
# Implementer note: while ExtensionDtype.empty is the public way to
2105
2128
# call this method, it is still required to implement this `_empty`
2106
2129
# method as well (it is called internally in pandas)
2107
- obj = cls ._from_sequence ([], dtype = dtype , fill_value = fill_value )
2130
+ obj = cls ._from_sequence ([], dtype = dtype )
2108
2131
2109
2132
taker = np .broadcast_to (np .intp (- 1 ), shape )
2110
2133
result = obj .take (taker , allow_fill = True )
0 commit comments