Skip to content

REF: tighten types on maybe_infer_to_datetimelike #41527

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
7 changes: 7 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,13 @@ def _try_cast(
subarr = construct_1d_object_array_from_listlike(arr)
return subarr

if dtype is None and isinstance(arr, list):
# filter out cases that we _dont_ want to go through maybe_cast_to_datetime
varr = np.array(arr, copy=False)
if varr.dtype != object or varr.size == 0:
return varr
arr = varr

try:
# GH#15832: Check if we are requesting a numeric dtype and
# that we can convert the data to the requested dtype.
Expand Down
28 changes: 16 additions & 12 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,9 @@ def maybe_castable(dtype: np.dtype) -> bool:
return dtype.name not in POSSIBLY_CAST_DTYPES


def maybe_infer_to_datetimelike(value: np.ndarray | list):
def maybe_infer_to_datetimelike(
value: np.ndarray,
) -> np.ndarray | DatetimeArray | TimedeltaArray:
"""
we might have a array (or single object) that is datetime like,
and no dtype is passed don't change the value unless we find a
Expand All @@ -1486,18 +1488,19 @@ def maybe_infer_to_datetimelike(value: np.ndarray | list):

Parameters
----------
value : np.ndarray or list
value : np.ndarray[object]

Returns
-------
np.ndarray, DatetimeArray, or TimedeltaArray

"""
if not isinstance(value, (np.ndarray, list)):
if not isinstance(value, np.ndarray) or value.dtype != object:
# Caller is responsible for passing only ndarray[object]
raise TypeError(type(value)) # pragma: no cover

v = np.array(value, copy=False)

# we only care about object dtypes
if not is_object_dtype(v.dtype):
return value

shape = v.shape
if v.ndim != 1:
v = v.ravel()
Expand Down Expand Up @@ -1575,6 +1578,8 @@ def maybe_cast_to_datetime(
"""
try to cast the array/value to a datetimelike dtype, converting float
nan to iNaT

We allow a list *only* when dtype is not None.
"""
from pandas.core.arrays.datetimes import sequence_to_datetimes
from pandas.core.arrays.timedeltas import sequence_to_td64ns
Expand Down Expand Up @@ -1666,11 +1671,10 @@ def maybe_cast_to_datetime(
value = maybe_infer_to_datetimelike(value)

elif isinstance(value, list):
# only do this if we have an array and the dtype of the array is not
# setup already we are not an integer/object, so don't bother with this
# conversion

value = maybe_infer_to_datetimelike(value)
# we only get here with dtype=None, which we do not allow
raise ValueError(
"maybe_cast_to_datetime allows a list *only* if dtype is not None"
)

return value

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ def ndarray_to_mgr(
if values.ndim == 2 and values.shape[0] != 1:
# transpose and separate blocks

dvals_list = [maybe_infer_to_datetimelike(row) for row in values]
dvals_list = [ensure_block_shape(dval, 2) for dval in dvals_list]
dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values]
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]

# TODO: What about re-joining object columns?
block_values = [
Expand Down