Skip to content

Commit e471c49

Browse files
committed
REF: _try_cast; go through fastpath more often, closes pandas-dev#28145
1 parent a246270 commit e471c49

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

pandas/core/construction.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
construct_1d_object_array_from_listlike,
4040
maybe_cast_to_datetime,
4141
maybe_cast_to_integer_array,
42-
maybe_castable,
4342
maybe_convert_platform,
4443
maybe_upcast,
44+
sanitize_to_nanoseconds,
4545
)
4646
from pandas.core.dtypes.common import (
4747
is_datetime64_ns_dtype,
@@ -664,30 +664,45 @@ def _try_cast(
664664
# perf shortcut as this is the most common case
665665
if (
666666
isinstance(arr, np.ndarray)
667-
and maybe_castable(arr.dtype)
667+
and arr.dtype != object
668668
and not copy
669669
and dtype is None
670670
):
671-
return arr
671+
return sanitize_to_nanoseconds(arr)
672672

673-
if isinstance(dtype, ExtensionDtype) and not isinstance(dtype, DatetimeTZDtype):
673+
if isinstance(dtype, ExtensionDtype):
674674
# create an extension array from its dtype
675675
# DatetimeTZ case needs to go through maybe_cast_to_datetime but
676676
# SparseDtype does not
677+
if isinstance(dtype, DatetimeTZDtype):
678+
# We can't go through _from_sequence because it handles dt64naive
679+
# data differently; _from_sequence treats naive as wall times,
680+
# while maybe_cast_to_datetime treats it as UTC
681+
# see test_maybe_promote_any_numpy_dtype_with_datetimetz
682+
683+
# error: Incompatible return value type (got "Union[ExtensionArray,
684+
# ndarray, List[Any]]", expected "Union[ExtensionArray, ndarray]")
685+
return maybe_cast_to_datetime(arr, dtype) # type: ignore[return-value]
686+
# TODO: copy?
687+
677688
array_type = dtype.construct_array_type()._from_sequence
678689
subarr = array_type(arr, dtype=dtype, copy=copy)
679690
return subarr
680691

681-
if is_object_dtype(dtype) and not isinstance(arr, np.ndarray):
682-
subarr = construct_1d_object_array_from_listlike(arr)
683-
return subarr
692+
elif is_object_dtype(dtype):
693+
if not isinstance(arr, np.ndarray):
694+
subarr = construct_1d_object_array_from_listlike(arr)
695+
return subarr
696+
return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy)
684697

685-
if dtype is None and isinstance(arr, list):
698+
elif dtype is None and isinstance(arr, list):
686699
# filter out cases that we _dont_ want to go through maybe_cast_to_datetime
687700
varr = np.array(arr, copy=False)
688701
if varr.dtype != object or varr.size == 0:
689702
return varr
690-
arr = varr
703+
# error: Incompatible return value type (got "Union[ExtensionArray,
704+
# ndarray, List[Any]]", expected "Union[ExtensionArray, ndarray]")
705+
return maybe_cast_to_datetime(varr, None) # type: ignore[return-value]
691706

692707
try:
693708
# GH#15832: Check if we are requesting a numeric dtype and

pandas/core/dtypes/cast.py

-17
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
from pandas.core.dtypes.common import (
4747
DT64NS_DTYPE,
48-
POSSIBLY_CAST_DTYPES,
4948
TD64NS_DTYPE,
5049
ensure_int8,
5150
ensure_int16,
@@ -58,7 +57,6 @@
5857
is_complex,
5958
is_complex_dtype,
6059
is_datetime64_dtype,
61-
is_datetime64_ns_dtype,
6260
is_datetime64tz_dtype,
6361
is_datetime_or_timedelta_dtype,
6462
is_dtype_equal,
@@ -73,7 +71,6 @@
7371
is_sparse,
7472
is_string_dtype,
7573
is_timedelta64_dtype,
76-
is_timedelta64_ns_dtype,
7774
is_unsigned_integer_dtype,
7875
pandas_dtype,
7976
)
@@ -1466,20 +1463,6 @@ def convert_dtypes(
14661463
return inferred_dtype
14671464

14681465

1469-
def maybe_castable(dtype: np.dtype) -> bool:
1470-
# return False to force a non-fastpath
1471-
1472-
# check datetime64[ns]/timedelta64[ns] are valid
1473-
# otherwise try to coerce
1474-
kind = dtype.kind
1475-
if kind == "M":
1476-
return is_datetime64_ns_dtype(dtype)
1477-
elif kind == "m":
1478-
return is_timedelta64_ns_dtype(dtype)
1479-
1480-
return dtype.name not in POSSIBLY_CAST_DTYPES
1481-
1482-
14831466
def maybe_infer_to_datetimelike(
14841467
value: np.ndarray,
14851468
) -> np.ndarray | DatetimeArray | TimedeltaArray:

pandas/core/dtypes/common.py

-15
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@
5858
is_sequence,
5959
)
6060

61-
POSSIBLY_CAST_DTYPES = {
62-
np.dtype(t).name
63-
for t in [
64-
"O",
65-
"int8",
66-
"uint8",
67-
"int16",
68-
"uint16",
69-
"int32",
70-
"uint32",
71-
"int64",
72-
"uint64",
73-
]
74-
}
75-
7661
DT64NS_DTYPE = conversion.DT64NS_DTYPE
7762
TD64NS_DTYPE = conversion.TD64NS_DTYPE
7863
INT64_DTYPE = np.dtype(np.int64)

0 commit comments

Comments
 (0)