Skip to content

REF: tighten signature on maybe_convert_platform #40209

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 1 commit into from
Mar 5, 2021
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
15 changes: 10 additions & 5 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
)
from pandas.core.dtypes.dtypes import IntervalDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeIndex,
ABCIntervalIndex,
ABCPeriodIndex,
ABCSeries,
)
from pandas.core.dtypes.missing import (
is_valid_na_for_dtype,
Expand Down Expand Up @@ -206,10 +206,7 @@ def __new__(
verify_integrity: bool = True,
):

if isinstance(data, (ABCSeries, ABCIntervalIndex)) and is_interval_dtype(
data.dtype
):
data = data._values # TODO: extract_array?
data = extract_array(data, extract_numpy=True)

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

return maybe_convert_platform(values)
19 changes: 12 additions & 7 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,21 @@
_int64_max = np.iinfo(np.int64).max


def maybe_convert_platform(values):
def maybe_convert_platform(
values: Union[list, tuple, range, np.ndarray, ExtensionArray]
) -> ArrayLike:
""" try to do platform conversion, allow ndarray or list here """
if isinstance(values, (list, tuple, range)):
values = construct_1d_object_array_from_listlike(values)
if getattr(values, "dtype", None) == np.object_:
if hasattr(values, "_values"):
values = values._values
values = lib.maybe_convert_objects(values)
arr = construct_1d_object_array_from_listlike(values)
else:
# The caller is responsible for ensuring that we have np.ndarray
# or ExtensionArray here.
arr = values

return values
if arr.dtype == object:
arr = lib.maybe_convert_objects(arr)

return arr


def is_nested_object(obj) -> bool:
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,15 @@ def _prep_ndarray(values, copy: bool = True) -> np.ndarray:
return arr[..., np.newaxis]

def convert(v):
return maybe_convert_platform(v)
if not is_list_like(v) or isinstance(v, ABCDataFrame):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a pretty general function (just commenting nothing to do now)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah im hoping we'll end up with just one sanitize_array-like function by the end of this process. the hard part at this point is deciding what we want to do about #40114, #40110, and #24435

return v
elif not hasattr(v, "dtype") and not isinstance(v, (list, tuple, range)):
# TODO: should we cast these to list?
return v

v = extract_array(v, extract_numpy=True)
res = maybe_convert_platform(v)
return res

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