ENH: Timestamp +- timedeltalike scalar support non-nano (#47313) · pandas-dev/pandas@b74dc5c (original) (raw)

`@@ -370,9 +370,6 @@ cdef class _Timestamp(ABCTimestamp):

`

370

370

` cdef:

`

371

371

` int64_t nanos = 0

`

372

372

``

373

``

`-

if isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:

`

374

``

`-

raise NotImplementedError(self._reso)

`

375

``

-

376

373

`if is_any_td_scalar(other):

`

377

374

`if is_timedelta64_object(other):

`

378

375

` other_reso = get_datetime64_unit(other)

`

`@@ -390,20 +387,31 @@ cdef class _Timestamp(ABCTimestamp):

`

390

387

`# TODO: no tests get here

`

391

388

` other = ensure_td64ns(other)

`

392

389

``

``

390

`+

TODO: what to do with mismatched resos?

`

393

391

`# TODO: disallow round_ok

`

394

392

` nanos = delta_to_nanoseconds(

`

395

393

` other, reso=self._reso, round_ok=True

`

396

394

` )

`

397

395

`try:

`

398

``

`-

result = type(self)(self.value + nanos, tz=self.tzinfo)

`

``

396

`+

new_value = self.value + nanos

`

399

397

`except OverflowError:

`

400

398

`# Use Python ints

`

401

399

`# Hit in test_tdi_add_overflow

`

402

``

`-

result = type(self)(int(self.value) + int(nanos), tz=self.tzinfo)

`

``

400

`+

new_value = int(self.value) + int(nanos)

`

``

401

+

``

402

`+

try:

`

``

403

`+

result = type(self)._from_value_and_reso(new_value, reso=self._reso, tz=self.tzinfo)

`

``

404

`+

except OverflowError as err:

`

``

405

`+

TODO: don't hard-code nanosecond here

`

``

406

`+

raise OutOfBoundsDatetime(f"Out of bounds nanosecond timestamp: {new_value}") from err

`

``

407

+

403

408

`if result is not NaT:

`

404

409

` result._set_freq(self._freq) # avoid warning in constructor

`

405

410

`return result

`

406

411

``

``

412

`+

elif isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:

`

``

413

`+

raise NotImplementedError(self._reso)

`

``

414

+

407

415

`elif is_integer_object(other):

`

408

416

`raise integer_op_not_supported(self)

`

409

417

``

`@@ -431,13 +439,16 @@ cdef class _Timestamp(ABCTimestamp):

`

431

439

`return NotImplemented

`

432

440

``

433

441

`def sub(self, other):

`

434

``

`-

if isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:

`

435

``

`-

raise NotImplementedError(self._reso)

`

``

442

`+

if other is NaT:

`

``

443

`+

return NaT

`

436

444

``

437

``

`-

if is_any_td_scalar(other) or is_integer_object(other):

`

``

445

`+

elif is_any_td_scalar(other) or is_integer_object(other):

`

438

446

` neg_other = -other

`

439

447

`return self + neg_other

`

440

448

``

``

449

`+

elif isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:

`

``

450

`+

raise NotImplementedError(self._reso)

`

``

451

+

441

452

`elif is_array(other):

`

442

453

`if other.dtype.kind in ['i', 'u']:

`

443

454

`raise integer_op_not_supported(self)

`

`@@ -450,9 +461,6 @@ cdef class _Timestamp(ABCTimestamp):

`

450

461

` )

`

451

462

`return NotImplemented

`

452

463

``

453

``

`-

if other is NaT:

`

454

``

`-

return NaT

`

455

``

-

456

464

`# coerce if necessary if we are a Timestamp-like

`

457

465

`if (PyDateTime_Check(self)

`

458

466

`and (PyDateTime_Check(other) or is_datetime64_object(other))):

`