From 85a82a109dfd5b37bbe972faee004704443972e7 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 8 Oct 2022 12:46:15 -0700 Subject: [PATCH 1/2] REF: avoid sanitize_to_nanoseconds --- pandas/core/construction.py | 5 +++-- pandas/core/dtypes/cast.py | 7 +------ pandas/core/groupby/grouper.py | 5 +++-- 3 files changed, 7 insertions(+), 10 deletions(-) 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..24221291f631e 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, @@ -560,7 +559,9 @@ def __init__( 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) + # 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})" From 980d7927a9aa631ed9b5ebf2e540092ba3979cb9 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 9 Oct 2022 13:34:02 -0700 Subject: [PATCH 2/2] fix test --- pandas/core/groupby/grouper.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 24221291f631e..948f911232143 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -557,11 +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 - # 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() + 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})"