Skip to content

Commit 860e2b7

Browse files
jbrockmendelim-vinicius
authored and
im-vinicius
committed
REF: privatize maybe_cast_to_extension_array (pandas-dev#53061)
1 parent d6c63c5 commit 860e2b7

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

pandas/core/arrays/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
validate_insert_loc,
3939
)
4040

41-
from pandas.core.dtypes.cast import maybe_cast_to_extension_array
41+
from pandas.core.dtypes.cast import maybe_cast_pointwise_result
4242
from pandas.core.dtypes.common import (
4343
is_list_like,
4444
is_scalar,
@@ -1957,7 +1957,7 @@ def _maybe_convert(arr):
19571957
# https://github.com/pandas-dev/pandas/issues/22850
19581958
# We catch all regular exceptions here, and fall back
19591959
# to an ndarray.
1960-
res = maybe_cast_to_extension_array(type(self), arr)
1960+
res = maybe_cast_pointwise_result(arr, self.dtype, same_dtype=False)
19611961
if not isinstance(res, type(self)):
19621962
# exception raised in _from_sequence; ensure we have ndarray
19631963
res = np.asarray(arr)

pandas/core/dtypes/cast.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
PeriodDtype,
6868
)
6969
from pandas.core.dtypes.generic import (
70-
ABCExtensionArray,
7170
ABCIndex,
7271
ABCSeries,
7372
)
@@ -463,17 +462,17 @@ def maybe_cast_pointwise_result(
463462

464463
cls = dtype.construct_array_type()
465464
if same_dtype:
466-
result = maybe_cast_to_extension_array(cls, result, dtype=dtype)
465+
result = _maybe_cast_to_extension_array(cls, result, dtype=dtype)
467466
else:
468-
result = maybe_cast_to_extension_array(cls, result)
467+
result = _maybe_cast_to_extension_array(cls, result)
469468

470469
elif (numeric_only and dtype.kind in "iufcb") or not numeric_only:
471470
result = maybe_downcast_to_dtype(result, dtype)
472471

473472
return result
474473

475474

476-
def maybe_cast_to_extension_array(
475+
def _maybe_cast_to_extension_array(
477476
cls: type[ExtensionArray], obj: ArrayLike, dtype: ExtensionDtype | None = None
478477
) -> ArrayLike:
479478
"""
@@ -492,10 +491,6 @@ def maybe_cast_to_extension_array(
492491
"""
493492
from pandas.core.arrays.string_ import BaseStringArray
494493

495-
assert isinstance(cls, type), f"must pass a type: {cls}"
496-
assertion_msg = f"must pass a subclass of ExtensionArray: {cls}"
497-
assert issubclass(cls, ABCExtensionArray), assertion_msg
498-
499494
# Everything can be converted to StringArrays, but we may not want to convert
500495
if issubclass(cls, BaseStringArray) and lib.infer_dtype(obj) != "string":
501496
return obj

0 commit comments

Comments
 (0)