Skip to content

Commit b15901e

Browse files
authored
CLN refactor maybe-castable (#39257)
1 parent fa6793a commit b15901e

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pandas/core/construction.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,13 @@ def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bo
588588
Otherwise an object array is returned.
589589
"""
590590
# perf shortcut as this is the most common case
591-
if isinstance(arr, np.ndarray):
592-
if maybe_castable(arr) and not copy and dtype is None:
593-
return arr
591+
if (
592+
isinstance(arr, np.ndarray)
593+
and maybe_castable(arr.dtype)
594+
and not copy
595+
and dtype is None
596+
):
597+
return arr
594598

595599
if isinstance(dtype, ExtensionDtype) and (dtype.kind != "M" or is_sparse(dtype)):
596600
# create an extension array from its dtype

pandas/core/dtypes/cast.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1331,20 +1331,18 @@ def convert_dtypes(
13311331
return inferred_dtype
13321332

13331333

1334-
def maybe_castable(arr: np.ndarray) -> bool:
1334+
def maybe_castable(dtype: np.dtype) -> bool:
13351335
# return False to force a non-fastpath
13361336

1337-
assert isinstance(arr, np.ndarray) # GH 37024
1338-
13391337
# check datetime64[ns]/timedelta64[ns] are valid
13401338
# otherwise try to coerce
1341-
kind = arr.dtype.kind
1339+
kind = dtype.kind
13421340
if kind == "M":
1343-
return is_datetime64_ns_dtype(arr.dtype)
1341+
return is_datetime64_ns_dtype(dtype)
13441342
elif kind == "m":
1345-
return is_timedelta64_ns_dtype(arr.dtype)
1343+
return is_timedelta64_ns_dtype(dtype)
13461344

1347-
return arr.dtype.name not in POSSIBLY_CAST_DTYPES
1345+
return dtype.name not in POSSIBLY_CAST_DTYPES
13481346

13491347

13501348
def maybe_infer_to_datetimelike(

0 commit comments

Comments
 (0)