Skip to content

Commit 6cec46c

Browse files
jbrockmendelTLouf
authored andcommitted
REF: _try_cast; go through fastpath more often (pandas-dev#41597)
1 parent e92fc00 commit 6cec46c

File tree

3 files changed

+30
-42
lines changed

3 files changed

+30
-42
lines changed

pandas/core/construction.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
construct_1d_object_array_from_listlike,
3939
maybe_cast_to_datetime,
4040
maybe_cast_to_integer_array,
41-
maybe_castable,
4241
maybe_convert_platform,
4342
maybe_upcast,
43+
sanitize_to_nanoseconds,
4444
)
4545
from pandas.core.dtypes.common import (
4646
is_datetime64_ns_dtype,
@@ -656,33 +656,53 @@ def _try_cast(
656656
-------
657657
np.ndarray or ExtensionArray
658658
"""
659+
is_ndarray = isinstance(arr, np.ndarray)
660+
659661
# perf shortcut as this is the most common case
662+
# Item "List[Any]" of "Union[List[Any], ndarray]" has no attribute "dtype"
660663
if (
661-
isinstance(arr, np.ndarray)
662-
and maybe_castable(arr.dtype)
664+
is_ndarray
665+
and arr.dtype != object # type: ignore[union-attr]
663666
and not copy
664667
and dtype is None
665668
):
666-
return arr
669+
# Argument 1 to "sanitize_to_nanoseconds" has incompatible type
670+
# "Union[List[Any], ndarray]"; expected "ndarray"
671+
return sanitize_to_nanoseconds(arr) # type: ignore[arg-type]
667672

668-
if isinstance(dtype, ExtensionDtype) and not isinstance(dtype, DatetimeTZDtype):
673+
if isinstance(dtype, ExtensionDtype):
669674
# create an extension array from its dtype
670675
# DatetimeTZ case needs to go through maybe_cast_to_datetime but
671676
# 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+
672688
array_type = dtype.construct_array_type()._from_sequence
673689
subarr = array_type(arr, dtype=dtype, copy=copy)
674690
return subarr
675691

676-
if is_object_dtype(dtype) and not isinstance(arr, np.ndarray):
677-
subarr = construct_1d_object_array_from_listlike(arr)
678-
return subarr
692+
elif is_object_dtype(dtype):
693+
if not is_ndarray:
694+
subarr = construct_1d_object_array_from_listlike(arr)
695+
return subarr
696+
return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy)
679697

680-
if dtype is None and isinstance(arr, list):
698+
elif dtype is None and not is_ndarray:
681699
# filter out cases that we _dont_ want to go through maybe_cast_to_datetime
682700
varr = np.array(arr, copy=False)
683701
if varr.dtype != object or varr.size == 0:
684702
return varr
685-
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]
686706

687707
try:
688708
# 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
)
@@ -1479,20 +1476,6 @@ def convert_dtypes(
14791476
return inferred_dtype
14801477

14811478

1482-
def maybe_castable(dtype: np.dtype) -> bool:
1483-
# return False to force a non-fastpath
1484-
1485-
# check datetime64[ns]/timedelta64[ns] are valid
1486-
# otherwise try to coerce
1487-
kind = dtype.kind
1488-
if kind == "M":
1489-
return is_datetime64_ns_dtype(dtype)
1490-
elif kind == "m":
1491-
return is_timedelta64_ns_dtype(dtype)
1492-
1493-
return dtype.name not in POSSIBLY_CAST_DTYPES
1494-
1495-
14961479
def maybe_infer_to_datetimelike(
14971480
value: np.ndarray,
14981481
) -> 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)