From 0a838a6a4dc0c3657a32091bfb42d1007329d087 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 3 May 2023 14:41:31 -0700 Subject: [PATCH] REF: privatize maybe_cast_to_extension_array --- pandas/core/arrays/base.py | 4 ++-- pandas/core/dtypes/cast.py | 11 +++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 165a421989002..27eb7994d3ccb 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -38,7 +38,7 @@ validate_insert_loc, ) -from pandas.core.dtypes.cast import maybe_cast_to_extension_array +from pandas.core.dtypes.cast import maybe_cast_pointwise_result from pandas.core.dtypes.common import ( is_list_like, is_scalar, @@ -1957,7 +1957,7 @@ def _maybe_convert(arr): # https://github.com/pandas-dev/pandas/issues/22850 # We catch all regular exceptions here, and fall back # to an ndarray. - res = maybe_cast_to_extension_array(type(self), arr) + res = maybe_cast_pointwise_result(arr, self.dtype, same_dtype=False) if not isinstance(res, type(self)): # exception raised in _from_sequence; ensure we have ndarray res = np.asarray(arr) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index db8bdd08ee112..d25e2fc675390 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -67,7 +67,6 @@ PeriodDtype, ) from pandas.core.dtypes.generic import ( - ABCExtensionArray, ABCIndex, ABCSeries, ) @@ -463,9 +462,9 @@ def maybe_cast_pointwise_result( cls = dtype.construct_array_type() if same_dtype: - result = maybe_cast_to_extension_array(cls, result, dtype=dtype) + result = _maybe_cast_to_extension_array(cls, result, dtype=dtype) else: - result = maybe_cast_to_extension_array(cls, result) + result = _maybe_cast_to_extension_array(cls, result) elif (numeric_only and dtype.kind in "iufcb") or not numeric_only: result = maybe_downcast_to_dtype(result, dtype) @@ -473,7 +472,7 @@ def maybe_cast_pointwise_result( return result -def maybe_cast_to_extension_array( +def _maybe_cast_to_extension_array( cls: type[ExtensionArray], obj: ArrayLike, dtype: ExtensionDtype | None = None ) -> ArrayLike: """ @@ -492,10 +491,6 @@ def maybe_cast_to_extension_array( """ from pandas.core.arrays.string_ import BaseStringArray - assert isinstance(cls, type), f"must pass a type: {cls}" - assertion_msg = f"must pass a subclass of ExtensionArray: {cls}" - assert issubclass(cls, ABCExtensionArray), assertion_msg - # Everything can be converted to StringArrays, but we may not want to convert if issubclass(cls, BaseStringArray) and lib.infer_dtype(obj) != "string": return obj