|
19 | 19 | cast,
|
20 | 20 | overload,
|
21 | 21 | )
|
22 |
| -import warnings |
23 | 22 |
|
24 | 23 | import numpy as np
|
25 | 24 |
|
|
35 | 34 | Substitution,
|
36 | 35 | cache_readonly,
|
37 | 36 | )
|
38 |
| -from pandas.util._exceptions import find_stack_level |
39 | 37 | from pandas.util._validators import (
|
40 | 38 | validate_bool_kwarg,
|
41 | 39 | validate_insert_loc,
|
42 | 40 | )
|
43 | 41 |
|
44 |
| -from pandas.core.dtypes.cast import maybe_cast_pointwise_result |
45 | 42 | from pandas.core.dtypes.common import (
|
46 | 43 | is_list_like,
|
47 | 44 | is_scalar,
|
|
89 | 86 | AstypeArg,
|
90 | 87 | AxisInt,
|
91 | 88 | Dtype,
|
92 |
| - DtypeObj, |
93 | 89 | FillnaOptions,
|
94 | 90 | InterpolateOptions,
|
95 | 91 | NumpySorter,
|
@@ -311,38 +307,6 @@ def _from_sequence(
|
311 | 307 | """
|
312 | 308 | raise AbstractMethodError(cls)
|
313 | 309 |
|
314 |
| - @classmethod |
315 |
| - def _from_scalars(cls, scalars, *, dtype: DtypeObj) -> Self: |
316 |
| - """ |
317 |
| - Strict analogue to _from_sequence, allowing only sequences of scalars |
318 |
| - that should be specifically inferred to the given dtype. |
319 |
| -
|
320 |
| - Parameters |
321 |
| - ---------- |
322 |
| - scalars : sequence |
323 |
| - dtype : ExtensionDtype |
324 |
| -
|
325 |
| - Raises |
326 |
| - ------ |
327 |
| - TypeError or ValueError |
328 |
| -
|
329 |
| - Notes |
330 |
| - ----- |
331 |
| - This is called in a try/except block when casting the result of a |
332 |
| - pointwise operation. |
333 |
| - """ |
334 |
| - try: |
335 |
| - return cls._from_sequence(scalars, dtype=dtype, copy=False) |
336 |
| - except (ValueError, TypeError): |
337 |
| - raise |
338 |
| - except Exception: |
339 |
| - warnings.warn( |
340 |
| - "_from_scalars should only raise ValueError or TypeError. " |
341 |
| - "Consider overriding _from_scalars where appropriate.", |
342 |
| - stacklevel=find_stack_level(), |
343 |
| - ) |
344 |
| - raise |
345 |
| - |
346 | 310 | @classmethod
|
347 | 311 | def _from_sequence_of_strings(
|
348 | 312 | cls, strings, *, dtype: ExtensionDtype, copy: bool = False
|
@@ -371,9 +335,6 @@ def _from_sequence_of_strings(
|
371 | 335 | from a sequence of scalars.
|
372 | 336 | api.extensions.ExtensionArray._from_factorized : Reconstruct an ExtensionArray
|
373 | 337 | after factorization.
|
374 |
| - api.extensions.ExtensionArray._from_scalars : Strict analogue to _from_sequence, |
375 |
| - allowing only sequences of scalars that should be specifically inferred to |
376 |
| - the given dtype. |
377 | 338 |
|
378 | 339 | Examples
|
379 | 340 | --------
|
@@ -416,6 +377,14 @@ def _from_factorized(cls, values, original):
|
416 | 377 | """
|
417 | 378 | raise AbstractMethodError(cls)
|
418 | 379 |
|
| 380 | + def _cast_pointwise_result(self, values) -> ArrayLike: |
| 381 | + """ |
| 382 | + Cast the result of a pointwise operation (e.g. Series.map) to an |
| 383 | + array, preserve dtype_backend if possible. |
| 384 | + """ |
| 385 | + values = np.asarray(values, dtype=object) |
| 386 | + return lib.maybe_convert_objects(values, convert_non_numeric=True) |
| 387 | + |
419 | 388 | # ------------------------------------------------------------------------
|
420 | 389 | # Must be a Sequence
|
421 | 390 | # ------------------------------------------------------------------------
|
@@ -2842,7 +2811,7 @@ def _maybe_convert(arr):
|
2842 | 2811 | # https://github.com/pandas-dev/pandas/issues/22850
|
2843 | 2812 | # We catch all regular exceptions here, and fall back
|
2844 | 2813 | # to an ndarray.
|
2845 |
| - res = maybe_cast_pointwise_result(arr, self.dtype, same_dtype=False) |
| 2814 | + res = self._cast_pointwise_result(arr) |
2846 | 2815 | if not isinstance(res, type(self)):
|
2847 | 2816 | # exception raised in _from_sequence; ensure we have ndarray
|
2848 | 2817 | res = np.asarray(arr)
|
|
0 commit comments