Skip to content

Commit 07aba3f

Browse files
jbrockmendelluckyvs1
authored andcommitted
REF: require listlike in maybe_cast_to_datetime (pandas-dev#38505)
1 parent 526519f commit 07aba3f

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

pandas/core/construction.py

+1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ def sanitize_array(
481481
return subarr
482482

483483
elif isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and len(data) > 0:
484+
# TODO: deque, array.array
484485
if isinstance(data, set):
485486
# Raise only for unordered sets, e.g., not for dict_keys
486487
raise TypeError("Set type is unordered")

pandas/core/dtypes/cast.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,9 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
13421342
from pandas.core.tools.datetimes import to_datetime
13431343
from pandas.core.tools.timedeltas import to_timedelta
13441344

1345+
if not is_list_like(value):
1346+
raise TypeError("value must be listlike")
1347+
13451348
if dtype is not None:
13461349
is_datetime64 = is_datetime64_dtype(dtype)
13471350
is_datetime64tz = is_datetime64tz_dtype(dtype)
@@ -1370,13 +1373,6 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
13701373
raise TypeError(
13711374
f"cannot convert datetimelike to dtype [{dtype}]"
13721375
)
1373-
elif is_datetime64tz:
1374-
1375-
# our NaT doesn't support tz's
1376-
# this will coerce to DatetimeIndex with
1377-
# a matching dtype below
1378-
if is_scalar(value) and isna(value):
1379-
value = [value]
13801376

13811377
elif is_timedelta64 and not is_dtype_equal(dtype, TD64NS_DTYPE):
13821378

@@ -1389,9 +1385,7 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
13891385
else:
13901386
raise TypeError(f"cannot convert timedeltalike to dtype [{dtype}]")
13911387

1392-
if is_scalar(value):
1393-
value = maybe_unbox_datetimelike(value, dtype)
1394-
elif not is_sparse(value):
1388+
if not is_sparse(value):
13951389
value = np.array(value, copy=False)
13961390

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

0 commit comments

Comments
 (0)