question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timedelta does not allow assignment from float

See original GitHub issue

When trying gh-4434, I ran into a force-cast difference between timedelta force casting and timedelta item assignment. There is also a discrepancy for float64 vs. python floats:

In [12]: np.array(3.).astype('timedelta64[D]')
Out[12]: array(datetime.timedelta(3), dtype='timedelta64[D]')

In [13]: np.array(3., dtype='timedelta64[D]')
ValueError: Could not convert object to NumPy timedelta

and:

In [17]: np.empty(1, dtype='timedelta64[D]')[[0]] = np.float64(3.)

In [18]: np.empty(1, dtype='timedelta64[D]')[0] = np.float64(3.)
ValueError: Could not convert object to NumPy timedelta

I could easily hack around this by adding an ellipsis, but this all seems a bit weird…

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:3
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
heavelockcommented, Dec 8, 2020

To all those previous observations I would also like to add that casting existing array of floats to np.timedelta64 results in flooring the values in the array, without throwing any error. Quite unfortunate.

np.__version__ # '1.19.4'
np.array([1.111]).astype('timedelta64[s]')  # array([1], dtype='timedelta64[s]')
np.array([1.999]).astype('timedelta64[s]')  # array([1], dtype='timedelta64[s]')
0reactions
eric-wiesercommented, Dec 8, 2020

Then I don’t think that’s any worse than array(1 .5).astype(int), which also silently rounds

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - TypeError: can't compare datetime.timedelta to float
I've given epgwidth a default value before the if statements for the case where the time difference is not falling in the 10-30...
Read more >
TimeDelta — Astropy v5.2
A TimeDelta object is initialized with one or more times in the val argument. The input times in val must conform to the...
Read more >
How to Fix: ValueError: cannot convert float NaN to integer
The way to fix this error is to deal with the NaN values before attempting to convert the column from a float to...
Read more >
TypeError: 'float' object does not support item assignment
The Python "TypeError: 'float' object does not support item assignment" occurs when we try to assign a value to a float using square ......
Read more >
What's new in 0.23.0 (May 15, 2018) - Pandas
Starting January 1, 2019, pandas feature releases will support Python 3 only. ... by zero should not depend on # whether the zero...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found