Skip to content

Commit 3e1d6d5

Browse files
authored
CLN: remove maybe_infer_to_datetimelike (#62209)
1 parent 0e21777 commit 3e1d6d5

File tree

3 files changed

+40
-53
lines changed

3 files changed

+40
-53
lines changed

pandas/core/construction.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
maybe_cast_to_datetime,
3232
maybe_cast_to_integer_array,
3333
maybe_convert_platform,
34-
maybe_infer_to_datetimelike,
3534
maybe_promote,
3635
)
3736
from pandas.core.dtypes.common import (
@@ -612,7 +611,15 @@ def sanitize_array(
612611
if dtype is None:
613612
subarr = data
614613
if data.dtype == object and infer_object:
615-
subarr = maybe_infer_to_datetimelike(data)
614+
subarr = lib.maybe_convert_objects(
615+
data,
616+
# Here we do not convert numeric dtypes, as if we wanted that,
617+
# numpy would have done it for us.
618+
convert_numeric=False,
619+
convert_non_numeric=True,
620+
convert_to_nullable_dtype=False,
621+
dtype_if_all_nat=np.dtype("M8[s]"),
622+
)
616623
elif data.dtype.kind == "U" and using_string_dtype():
617624
from pandas.core.arrays.string_ import StringDtype
618625

@@ -659,7 +666,15 @@ def sanitize_array(
659666
subarr = maybe_convert_platform(data)
660667
if subarr.dtype == object:
661668
subarr = cast(np.ndarray, subarr)
662-
subarr = maybe_infer_to_datetimelike(subarr)
669+
subarr = lib.maybe_convert_objects(
670+
subarr,
671+
# Here we do not convert numeric dtypes, as if we wanted that,
672+
# numpy would have done it for us.
673+
convert_numeric=False,
674+
convert_non_numeric=True,
675+
convert_to_nullable_dtype=False,
676+
dtype_if_all_nat=np.dtype("M8[s]"),
677+
)
663678

664679
subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d)
665680

pandas/core/dtypes/cast.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
DtypeObj,
9898
NumpyIndexT,
9999
Scalar,
100-
npt,
101100
)
102101

103102
from pandas import Index
@@ -1058,51 +1057,6 @@ def convert_dtypes(
10581057
return inferred_dtype # type: ignore[return-value]
10591058

10601059

1061-
def maybe_infer_to_datetimelike(
1062-
value: npt.NDArray[np.object_],
1063-
convert_to_nullable_dtype: bool = False,
1064-
) -> np.ndarray | DatetimeArray | TimedeltaArray | PeriodArray | IntervalArray:
1065-
"""
1066-
we might have a array (or single object) that is datetime like,
1067-
and no dtype is passed don't change the value unless we find a
1068-
datetime/timedelta set
1069-
1070-
this is pretty strict in that a datetime/timedelta is REQUIRED
1071-
in addition to possible nulls/string likes
1072-
1073-
Parameters
1074-
----------
1075-
value : np.ndarray[object]
1076-
1077-
Returns
1078-
-------
1079-
np.ndarray, DatetimeArray, TimedeltaArray, PeriodArray, or IntervalArray
1080-
1081-
"""
1082-
if not isinstance(value, np.ndarray) or value.dtype != object:
1083-
# Caller is responsible for passing only ndarray[object]
1084-
raise TypeError(type(value)) # pragma: no cover
1085-
if value.ndim != 1:
1086-
# Caller is responsible
1087-
raise ValueError(value.ndim) # pragma: no cover
1088-
1089-
if not len(value):
1090-
return value
1091-
1092-
# error: Incompatible return value type (got "Union[ExtensionArray,
1093-
# ndarray[Any, Any]]", expected "Union[ndarray[Any, Any], DatetimeArray,
1094-
# TimedeltaArray, PeriodArray, IntervalArray]")
1095-
return lib.maybe_convert_objects( # type: ignore[return-value]
1096-
value,
1097-
# Here we do not convert numeric dtypes, as if we wanted that,
1098-
# numpy would have done it for us.
1099-
convert_numeric=False,
1100-
convert_non_numeric=True,
1101-
convert_to_nullable_dtype=convert_to_nullable_dtype,
1102-
dtype_if_all_nat=np.dtype("M8[s]"),
1103-
)
1104-
1105-
11061060
def maybe_cast_to_datetime(
11071061
value: np.ndarray | list, dtype: np.dtype
11081062
) -> DatetimeArray | TimedeltaArray | np.ndarray:

pandas/core/internals/construction.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
dict_compat,
2525
maybe_cast_to_datetime,
2626
maybe_convert_platform,
27-
maybe_infer_to_datetimelike,
2827
)
2928
from pandas.core.dtypes.common import (
3029
is_1d_only_ea_dtype,
@@ -290,7 +289,18 @@ def ndarray_to_mgr(
290289
# embedded in an object type
291290
if dtype is None and infer_object and is_object_dtype(values.dtype):
292291
obj_columns = list(values)
293-
maybe_datetime = [maybe_infer_to_datetimelike(x) for x in obj_columns]
292+
maybe_datetime = [
293+
lib.maybe_convert_objects(
294+
x,
295+
# Here we do not convert numeric dtypes, as if we wanted that,
296+
# numpy would have done it for us.
297+
convert_numeric=False,
298+
convert_non_numeric=True,
299+
convert_to_nullable_dtype=False,
300+
dtype_if_all_nat=np.dtype("M8[s]"),
301+
)
302+
for x in obj_columns
303+
]
294304
# don't convert (and copy) the objects if no type inference occurs
295305
if any(x is not y for x, y in zip(obj_columns, maybe_datetime)):
296306
block_values = [
@@ -485,7 +495,7 @@ def convert(v):
485495

486496
v = extract_array(v, extract_numpy=True)
487497
res = maybe_convert_platform(v)
488-
# We don't do maybe_infer_to_datetimelike here bc we will end up doing
498+
# We don't do maybe_infer_objects here bc we will end up doing
489499
# it column-by-column in ndarray_to_mgr
490500
return res
491501

@@ -965,7 +975,15 @@ def convert(arr):
965975
if arr.dtype == np.dtype("O"):
966976
# i.e. maybe_convert_objects didn't convert
967977
convert_to_nullable_dtype = dtype_backend != "numpy"
968-
arr = maybe_infer_to_datetimelike(arr, convert_to_nullable_dtype)
978+
arr = lib.maybe_convert_objects(
979+
arr,
980+
# Here we do not convert numeric dtypes, as if we wanted that,
981+
# numpy would have done it for us.
982+
convert_numeric=False,
983+
convert_non_numeric=True,
984+
convert_to_nullable_dtype=convert_to_nullable_dtype,
985+
dtype_if_all_nat=np.dtype("M8[s]"),
986+
)
969987
if convert_to_nullable_dtype and arr.dtype == np.dtype("O"):
970988
new_dtype = StringDtype()
971989
arr_cls = new_dtype.construct_array_type()

0 commit comments

Comments
 (0)