Skip to content

timedelta64 only accepts int timedeltas #7617

Closed

Description

@nikolauspschuetz

timedelta64 only accepts timedeltas of type int; it does not support timedeltas of other int types, such as int64.

Activity

  1. shoyer commented on May 11, 2016

    @shoyer
    Member

    Yep, this is definitely a bug. To reproduce:

    In [23]: np.timedelta64(np.int64(123), 'us')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-23-4c4ef02c5760> in <module>()
    ----> 1 np.timedelta64(np.int64(123), 'us')
    
    ValueError: Could not convert object to NumPy timedelta
    
  2. yashmehrotra commented on May 11, 2016

    @yashmehrotra
    Contributor

    @shoyer Hi, I was looking into this bug and found that it works fine in python2 but throws an error in python3. The reason is, the function PyInt_Check is overrided in python3 , and it returns false when we check for raw integers.

    The overrided PyInt_Check is returning false, as it internally checks whether the value returns true for PyLong_Check here.

    What do you suggest should be done here ? ( I would like to work on this bugfix)

    P.S. : We also get the same error when we call np.datetime (Due to the same flow)

  3. shoyer commented on May 11, 2016

    @shoyer
    Member

    It looks like PyArray_IsScalar(object, Integer) may be the right check for NumPy integer scalars, but I'm not 100% sure since the documentation for that function is rather terse.

    @seberg How do you do type checks for integers in indexing operations?

  4. seberg commented on May 11, 2016

    @seberg
    Member

    I only check if it can cast with "same type" to intp IIRC. For scalars, I try PyArray_PyIntAsIntp, which I changed to raise an error when operator.index fails or it is a bool.

  5. seberg commented on May 11, 2016

    @seberg
    Member

    There is an unfortunate thing here, since intp is too small for datetime on some platforms, you cannot use the operator.index trick here I guess :(.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions