Skip to content

ENH: overflow-safe astype for datetime64/timedelta64 unit conversion #16352

Closed

Description

@jbrockmendel
import numpy as np
from pandas._libs.tslibs.conversion import ensure_datetime64ns
arr = np.array(["2367-12-31 12:00:00"], dtype="datetime64[h]")

>>> arr.astype("datetime64[ns]")
array(['1783-06-11T12:25:26.290448384'], dtype='datetime64[ns]')

>>> ensure_datetime64ns(arr)
[...]
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2367-12-31 12:00:00

Is there a compelling reason numpy doesnt do this?

Activity

  1. seberg commented on May 22, 2020

    @seberg
    Member

    No, we should raise it. It is a bit clumsy to raise errors from within the casting machinery currently, I think that is pretty much the only reason...

    EDIT: There could be a point of just returning NaT, although not really better IMO.

  2. jbrockmendel commented on Jun 17, 2020

    @jbrockmendel
    ContributorAuthor

    In addition to astype, it'd be nice if addition/subtraction were overflow-safe

    np.seterr(over="raise")
    
    arr = np.arange(10)*10**18
    arr2 = arr.view("datetime64[ns]")
    arr3 = arr.view("timedelta64[ns]")
    
    >>> arr2 + arr3
    array(['1970-01-01T00:00:00.000000000', '2033-05-18T03:33:20.000000000',
           '2096-10-02T07:06:40.000000000', '2160-02-18T10:40:00.000000000',
           '2223-07-06T14:13:20.000000000', '1702-05-02T18:12:06.290448384',
           '1765-09-16T21:45:26.290448384', '1829-02-02T01:18:46.290448384',
           '1892-06-19T04:52:06.290448384', '1955-11-05T08:25:26.290448384'],
          dtype='datetime64[ns]')
    
    
  3. jbrockmendel commented on Mar 23, 2022

    @jbrockmendel
    ContributorAuthor

    Just implemented a version of this in pandas-dev/pandas#46478 that I'm hoping we can upstream to numpy somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions