Skip to content

REF: privatize maybe_cast_to_extension_array #53061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
PeriodDtype,
)
from pandas.core.dtypes.generic import (
ABCExtensionArray,
ABCIndex,
ABCSeries,
)
Expand Down Expand Up @@ -463,17 +462,17 @@ 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)

return result


def maybe_cast_to_extension_array(
def _maybe_cast_to_extension_array(
cls: type[ExtensionArray], obj: ArrayLike, dtype: ExtensionDtype | None = None
) -> ArrayLike:
"""
Expand All @@ -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
Expand Down