Skip to content

Commit fd0bd91

Browse files
authored
REF: tighten signature on maybe_convert_platform (#40209)
1 parent 539b813 commit fd0bd91

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

Diff for: pandas/core/arrays/interval.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
)
5454
from pandas.core.dtypes.dtypes import IntervalDtype
5555
from pandas.core.dtypes.generic import (
56+
ABCDataFrame,
5657
ABCDatetimeIndex,
5758
ABCIntervalIndex,
5859
ABCPeriodIndex,
59-
ABCSeries,
6060
)
6161
from pandas.core.dtypes.missing import (
6262
is_valid_na_for_dtype,
@@ -206,10 +206,7 @@ def __new__(
206206
verify_integrity: bool = True,
207207
):
208208

209-
if isinstance(data, (ABCSeries, ABCIntervalIndex)) and is_interval_dtype(
210-
data.dtype
211-
):
212-
data = data._values # TODO: extract_array?
209+
data = extract_array(data, extract_numpy=True)
213210

214211
if isinstance(data, cls):
215212
left = data._left
@@ -1616,7 +1613,15 @@ def _maybe_convert_platform_interval(values) -> ArrayLike:
16161613
# empty lists/tuples get object dtype by default, but this is
16171614
# prohibited for IntervalArray, so coerce to integer instead
16181615
return np.array([], dtype=np.int64)
1616+
elif not is_list_like(values) or isinstance(values, ABCDataFrame):
1617+
# This will raise later, but we avoid passing to maybe_convert_platform
1618+
return values
16191619
elif is_categorical_dtype(values):
16201620
values = np.asarray(values)
1621+
elif not hasattr(values, "dtype") and not isinstance(values, (list, tuple, range)):
1622+
# TODO: should we just cast these to list?
1623+
return values
1624+
else:
1625+
values = extract_array(values, extract_numpy=True)
16211626

16221627
return maybe_convert_platform(values)

Diff for: pandas/core/dtypes/cast.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,21 @@
118118
_int64_max = np.iinfo(np.int64).max
119119

120120

121-
def maybe_convert_platform(values):
121+
def maybe_convert_platform(
122+
values: Union[list, tuple, range, np.ndarray, ExtensionArray]
123+
) -> ArrayLike:
122124
""" try to do platform conversion, allow ndarray or list here """
123125
if isinstance(values, (list, tuple, range)):
124-
values = construct_1d_object_array_from_listlike(values)
125-
if getattr(values, "dtype", None) == np.object_:
126-
if hasattr(values, "_values"):
127-
values = values._values
128-
values = lib.maybe_convert_objects(values)
126+
arr = construct_1d_object_array_from_listlike(values)
127+
else:
128+
# The caller is responsible for ensuring that we have np.ndarray
129+
# or ExtensionArray here.
130+
arr = values
129131

130-
return values
132+
if arr.dtype == object:
133+
arr = lib.maybe_convert_objects(arr)
134+
135+
return arr
131136

132137

133138
def is_nested_object(obj) -> bool:

Diff for: pandas/core/internals/construction.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,15 @@ def _prep_ndarray(values, copy: bool = True) -> np.ndarray:
424424
return arr[..., np.newaxis]
425425

426426
def convert(v):
427-
return maybe_convert_platform(v)
427+
if not is_list_like(v) or isinstance(v, ABCDataFrame):
428+
return v
429+
elif not hasattr(v, "dtype") and not isinstance(v, (list, tuple, range)):
430+
# TODO: should we cast these to list?
431+
return v
432+
433+
v = extract_array(v, extract_numpy=True)
434+
res = maybe_convert_platform(v)
435+
return res
428436

429437
# we could have a 1-dim or 2-dim list here
430438
# this is equiv of np.asarray, but does object conversion

0 commit comments

Comments
 (0)