Skip to content

REF: avoid sanitize_to_nanoseconds #49009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So Series(...).to_numpy() will coerce to nanos and then output to numpy? Is this a lot slower than sanitize_to_nanoseconds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So Series(...).to_numpy() will coerce to nanos and then output to numpy?

Yes.

Is this a lot slower than sanitize_to_nanoseconds?

A bit. Adds about 65 microseconds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I guess that's a fair tradeoff for being able to clean up sanitize_to_nanoseconds in #48670


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