Skip to content

REF: require listlike in maybe_cast_to_datetime #38505

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 8 commits into from
Dec 22, 2020
1 change: 1 addition & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def sanitize_array(
return subarr

elif isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and len(data) > 0:
# TODO: deque, array.array
if isinstance(data, set):
# Raise only for unordered sets, e.g., not for dict_keys
raise TypeError("Set type is unordered")
Expand Down
14 changes: 4 additions & 10 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,9 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
from pandas.core.tools.datetimes import to_datetime
from pandas.core.tools.timedeltas import to_timedelta

if not is_list_like(value):
raise TypeError("value must be listlike")

if dtype is not None:
is_datetime64 = is_datetime64_dtype(dtype)
is_datetime64tz = is_datetime64tz_dtype(dtype)
Expand Down Expand Up @@ -1367,13 +1370,6 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
raise TypeError(
f"cannot convert datetimelike to dtype [{dtype}]"
)
elif is_datetime64tz:

# our NaT doesn't support tz's
# this will coerce to DatetimeIndex with
# a matching dtype below
if is_scalar(value) and isna(value):
value = [value]

elif is_timedelta64 and not is_dtype_equal(dtype, TD64NS_DTYPE):

Expand All @@ -1386,9 +1382,7 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
else:
raise TypeError(f"cannot convert timedeltalike to dtype [{dtype}]")

if is_scalar(value):
value = maybe_unbox_datetimelike(value, dtype)
elif not is_sparse(value):
if not is_sparse(value):
value = np.array(value, copy=False)

# have a scalar array-like (e.g. NaT)
Expand Down