diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 88e82a67ab854..989ce0335a476 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -45,7 +45,6 @@ maybe_convert_platform, maybe_infer_to_datetimelike, maybe_upcast, - sanitize_to_nanoseconds, ) from pandas.core.dtypes.common import ( is_datetime64_ns_dtype, @@ -782,7 +781,9 @@ def _try_cast( if is_ndarray: arr = cast(np.ndarray, arr) if arr.dtype != object: - return sanitize_to_nanoseconds(arr, copy=copy) + if copy: + return arr.copy() + return arr out = maybe_infer_to_datetimelike(arr) if out is arr and copy: diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 1a5dc95199e22..6b890f98e8cac 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1423,12 +1423,7 @@ def maybe_cast_to_datetime( return astype_nansafe(value, dtype) # type: ignore[arg-type] elif isinstance(value, np.ndarray): - if value.dtype.kind in ["M", "m"]: - # catch a datetime/timedelta that is not of ns variety - # and no coercion specified - value = sanitize_to_nanoseconds(value) - - elif value.dtype == _dtype_obj: + if value.dtype == _dtype_obj: value = maybe_infer_to_datetimelike(value) elif isinstance(value, list): diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 40c3c88b94e9e..948f911232143 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -26,7 +26,6 @@ from pandas.util._decorators import cache_readonly from pandas.util._exceptions import find_stack_level -from pandas.core.dtypes.cast import sanitize_to_nanoseconds from pandas.core.dtypes.common import ( is_categorical_dtype, is_list_like, @@ -558,9 +557,12 @@ def __init__( raise AssertionError(errmsg) if isinstance(self.grouping_vector, np.ndarray): - # if we have a date/time-like grouper, make sure that we have - # Timestamps like - self.grouping_vector = sanitize_to_nanoseconds(self.grouping_vector) + if self.grouping_vector.dtype.kind in ["m", "M"]: + # if we have a date/time-like grouper, make sure that we have + # Timestamps like + # TODO 2022-10-08 we only have one test that gets here and + # values are already in nanoseconds in that case. + self.grouping_vector = Series(self.grouping_vector).to_numpy() def __repr__(self) -> str: return f"Grouping({self.name})"