Skip to content

np.vectorize handling of timedelta64 arrays #5180

Closed

Description

@behzadnouri

It seems that np.vectorize casts / unboxes timedelta64 values and does so kind of inconsistently depending on the unit. I am not sure that this was intentional, but makes it difficult to work with such arrays; in particular, it is not possible to vectorize a function which takes timedelta64 value;

>>> np.__version__
'1.9.0'
>>> f = lambda x: print(type(x), x)
>>> xs = np.arange(1, 3).astype('timedelta64[h]')
>>> xs
array([1, 2], dtype='timedelta64[h]')

with [h] and [s] unit:

>>> _ = np.vectorize(f)(xs)
<class 'numpy.timedelta64'> 1 hours
<class 'datetime.timedelta'> 1:00:00
<class 'datetime.timedelta'> 2:00:00
>>> _ = np.vectorize(f)(xs.astype('timedelta64[s]'))
<class 'numpy.timedelta64'> 3600 seconds
<class 'datetime.timedelta'> 1:00:00
<class 'datetime.timedelta'> 2:00:00
>>> _ = np.vectorize(f, otypes='O')(xs.astype('timedelta64[s]'))
<class 'datetime.timedelta'> 1:00:00
<class 'datetime.timedelta'> 2:00:00

with [ns] unit:

>>> _ = np.vectorize(f)(xs.astype('timedelta64[ns]'))
<class 'numpy.timedelta64'> 3600000000000 nanoseconds
<class 'int'> 3600000000000
<class 'int'> 7200000000000
>>> _ = np.vectorize(f, otypes='O')(xs.astype('timedelta64[ns]'))
<class 'int'> 3600000000000
<class 'int'> 7200000000000

Activity

  1. seberg commented on Jan 3, 2019

    @seberg
    Member

    Closing in favor of similar/duplicate (newer) issue gh-12550

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions