timedelta error with numpy.longdouble dtype

12,766

Solution 1

So maybe timedelta for dtype np.longdouble isn't implemented?

In short, yes.

From the documentation:

class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative.

Here "long" refers to a Python long integer, not a longdouble float.


Update

I think I've figured the seemingly inconsistent behavior of np.float64. What seems to be relevant is whether or not the numpy dtype subclasses one of the native Python scalar types that timedelta accepts.

On my 64 bit machine, running Python 2.7.9, numpy v1.10.1:

In [1]: timedelta(np.float64(1))
Out[1]: datetime.timedelta(1)

In [2]: timedelta(np.float32(1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4a7874ba393b> in <module>()
----> 1 timedelta(np.float32(1))

TypeError: unsupported type for timedelta days component: numpy.float32

In [3]: timedelta(np.int64(1))
Out[3]: datetime.timedelta(1)

In [4]: timedelta(np.int32(1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-0475c6c8f1aa> in <module>()
----> 1 timedelta(np.int32(1))

TypeError: unsupported type for timedelta days component: numpy.int32

In [5]: issubclass(np.float64, float)
Out[5]: True

In [6]: issubclass(np.float32, float)
Out[6]: False

In [7]: issubclass(np.int64, int)
Out[7]: True

In [8]: issubclass(np.int32, int)
Out[8]: False

However the OP reported in the comments that timedelta(np.int64(1)) did not work using Python 3.4.3. I think that is due to the fact that when numpy is built on Python 3x, np.int64 no longer subclasses int.

Here's what happens in Python 3.4.3:

In [1]: timedelta(np.int64(1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-9902ea26a52d> in <module>()
----> 1 timedelta(np.int64(1))

TypeError: unsupported type for timedelta days component: numpy.int64

In [2]: issubclass(np.int64, int)
Out[2]: False

Solution 2

I ran into a similar issue where

timedelta(days = value.astype(int)) did not work but

timedelta(days = int(value)) worked.

I hope it is useful for future Googlers.

Solution 3

On Anaconda64, Win7, Python 2.7.11, NumPy 1.10.1, timedelta's response to numpy.int32 depends on the value:

In [3]: datetime.timedelta(seconds = numpy.int32(2147))
Out[3]: datetime.timedelta(0, 2147)

In [4]: datetime.timedelta(seconds = numpy.int32(2148))
Out[4]: datetime.timedelta(-1, 84253, 32704)

Full log from Spyder startup:

Python 2.7.11 |Anaconda 2.1.0 (64-bit)| (default, Dec  7 2015, 14:10:42) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
%guiref   -> A brief reference about the graphical user interface.
In [1]: import numpy

In [2]: import datetime

In [3]: datetime.timedelta(seconds = numpy.int32(2147))

Out[3]: datetime.timedelta(0, 2147)

In [4]: datetime.timedelta(seconds = numpy.int32(2148))

Out[4]: datetime.timedelta(-1, 84253, 32704)
Share:
12,766

Related videos on Youtube

Anton Protopopov
Author by

Anton Protopopov

Updated on June 04, 2022

Comments

  • Anton Protopopov
    Anton Protopopov almost 2 years

    I have times with dtype numpy.longdouble and when I'm trying to use that values with timedelta function I've got errors. But when I convert it to numpy.float64 everything is fine. Could somebody explain that behaviour?

    import numpy as np
    from datetime import timedelta
    t1 = np.array([1000], dtype=np.longdouble)
    t2 = np.array([1000], dtype=np.float64)
    
    In [166]: timedelta(seconds=t1[0])
    TypeError: unsupported type for timedelta seconds component: numpy.float64
    
    In [167]: timedelta(seconds=t2[0])
    Out[167]: datetime.timedelta(0, 1000)
    
    In [168]: timedelta(seconds=t1[0].astype(np.float64))
    Out[168]: datetime.timedelta(0, 1000)
    

    When I'm trying to see dtypes of variables they are look the similar but not the same:

    In [172]: t1[0].dtype
    Out[172]: dtype('float64')
    
    In [173]: t2[0].dtype
    Out[173]: dtype('float64')
    
    In [174]: np.longdouble == np.float64
    Out[174]: False
    
    In [175]: t1[0].dtype == t2[0].dtype
    Out[175]: True
    

    Edit

    And it's strange that it's not working for np.int32 and np.int64 either:

    t3 = np.array([1000], dtype=np.int32)
    t4 = np.array([1000], dtype=np.int64)
    
    In [29]: timedelta(t3[0])
    TypeError: unsupported type for timedelta days component: numpy.int32
    
    In [69]: timedelta(t4[0])
    TypeError: unsupported type for timedelta days component: numpy.int64
    
  • Anton Protopopov
    Anton Protopopov over 8 years
    that's really strange. I thought python is platform independent. Which version of numpy and python are you using? Mine: numpy: 1.10.1; Python 3.4.3
  • ali_m
    ali_m over 8 years
    Yes, it does seem to be Python3-specific behavior. See my update.