Skip to content
Advertisement

timedelta error with numpy.longdouble dtype

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?

JavaScript

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

JavaScript

Edit

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

JavaScript

Advertisement

Answer

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement