Skip to content

Commit 32bcaa9

Browse files
committed
woops almost duplicate of pandas-dev#49015
2 parents 51feea5 + 582377f commit 32bcaa9

File tree

6 files changed

+19
-30
lines changed

6 files changed

+19
-30
lines changed

pandas/core/arrays/datetimelike.py

-1
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,6 @@ def _add_timedeltalike_scalar(self, other):
12761276
# PeriodArray overrides, so we only get here with DTA/TDA
12771277
self = cast("DatetimeArray | TimedeltaArray", self)
12781278
other = Timedelta(other)
1279-
12801279
self, other = self._ensure_matching_resos(other)
12811280
return self._add_timedeltalike(other)
12821281

pandas/core/construction.py

+3-9
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,
@@ -325,13 +324,6 @@ def array(
325324

326325
data = extract_array(data, extract_numpy=True)
327326

328-
if isinstance(data, ExtensionArray) and (
329-
dtype is None or is_dtype_equal(dtype, data.dtype)
330-
):
331-
if copy:
332-
return data.copy()
333-
return data
334-
335327
# this returns None for not-found dtypes.
336328
if isinstance(dtype, str):
337329
dtype = registry.find(dtype) or dtype
@@ -789,7 +781,9 @@ def _try_cast(
789781
if is_ndarray:
790782
arr = cast(np.ndarray, arr)
791783
if arr.dtype != object:
792-
return sanitize_to_nanoseconds(arr, copy=copy)
784+
if copy:
785+
return arr.copy()
786+
return arr
793787

794788
out = maybe_infer_to_datetimelike(arr)
795789
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})"

pandas/tests/scalar/timedelta/test_timedelta.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ def test_truediv_timedeltalike(self, td):
183183
assert (2.5 * td) / td == 2.5
184184

185185
other = Timedelta(td.value)
186-
187-
msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow"
186+
msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow."
188187
with pytest.raises(OutOfBoundsTimedelta, match=msg):
189188
td / other
190189

pandas/tests/tseries/offsets/test_offsets.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from pandas._libs.tslibs import (
2121
NaT,
22+
Timedelta,
2223
Timestamp,
2324
conversion,
2425
timezones,
@@ -566,6 +567,12 @@ def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request):
566567
expected = dti._data + off
567568
result = dta + off
568569

570+
exp_unit = unit
571+
if isinstance(off, Tick) and off._reso > dta._reso:
572+
# cast to higher reso like we would with Timedelta scalar
573+
exp_unit = Timedelta(off)._unit
574+
expected = expected._as_unit(exp_unit)
575+
569576
if len(w):
570577
# PerformanceWarning was issued bc _apply_array raised, so we
571578
# fell back to object dtype, for which the code path does
@@ -576,14 +583,7 @@ def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request):
576583
)
577584
request.node.add_marker(mark)
578585

579-
# result.dtype should match M8[{unit}], while expected.dtype should
580-
# always be in nanos
581-
# (this is for cases when offset_types is not itself Nano)
582-
assert result.dtype != expected.dtype
583-
584-
tm.assert_numpy_array_equal(
585-
result._ndarray, expected._ndarray.astype(result.dtype)
586-
)
586+
tm.assert_numpy_array_equal(result._ndarray, expected._ndarray)
587587

588588

589589
class TestDateOffset(Base):

0 commit comments

Comments
 (0)