Skip to content

Commit 582377f

Browse files
authored
REF: avoid sanitize_to_nanoseconds (#49009)
* REF: avoid sanitize_to_nanoseconds * fix test
1 parent f3f675c commit 582377f

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

pandas/core/construction.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
maybe_convert_platform,
4646
maybe_infer_to_datetimelike,
4747
maybe_upcast,
48-
sanitize_to_nanoseconds,
4948
)
5049
from pandas.core.dtypes.common import (
5150
is_datetime64_ns_dtype,
@@ -782,7 +781,9 @@ def _try_cast(
782781
if is_ndarray:
783782
arr = cast(np.ndarray, arr)
784783
if arr.dtype != object:
785-
return sanitize_to_nanoseconds(arr, copy=copy)
784+
if copy:
785+
return arr.copy()
786+
return arr
786787

787788
out = maybe_infer_to_datetimelike(arr)
788789
if out is arr and copy:

pandas/core/dtypes/cast.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -1423,12 +1423,7 @@ def maybe_cast_to_datetime(
14231423
return astype_nansafe(value, dtype) # type: ignore[arg-type]
14241424

14251425
elif isinstance(value, np.ndarray):
1426-
if value.dtype.kind in ["M", "m"]:
1427-
# catch a datetime/timedelta that is not of ns variety
1428-
# and no coercion specified
1429-
value = sanitize_to_nanoseconds(value)
1430-
1431-
elif value.dtype == _dtype_obj:
1426+
if value.dtype == _dtype_obj:
14321427
value = maybe_infer_to_datetimelike(value)
14331428

14341429
elif isinstance(value, list):

pandas/core/groupby/grouper.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from pandas.util._decorators import cache_readonly
2727
from pandas.util._exceptions import find_stack_level
2828

29-
from pandas.core.dtypes.cast import sanitize_to_nanoseconds
3029
from pandas.core.dtypes.common import (
3130
is_categorical_dtype,
3231
is_list_like,
@@ -558,9 +557,12 @@ def __init__(
558557
raise AssertionError(errmsg)
559558

560559
if isinstance(self.grouping_vector, np.ndarray):
561-
# if we have a date/time-like grouper, make sure that we have
562-
# Timestamps like
563-
self.grouping_vector = sanitize_to_nanoseconds(self.grouping_vector)
560+
if self.grouping_vector.dtype.kind in ["m", "M"]:
561+
# if we have a date/time-like grouper, make sure that we have
562+
# Timestamps like
563+
# TODO 2022-10-08 we only have one test that gets here and
564+
# values are already in nanoseconds in that case.
565+
self.grouping_vector = Series(self.grouping_vector).to_numpy()
564566

565567
def __repr__(self) -> str:
566568
return f"Grouping({self.name})"

0 commit comments

Comments
 (0)