BUG: .item() on a 0-dimensional datetime64[ns] array yields an integer
See original GitHub issueThis is obviously not useful:
In [4]: np.array(np.datetime64('2000-01-01', 'us')).item()
Out[4]: datetime.datetime(2000, 1, 1, 0, 0)
In [5]: np.array(np.datetime64('2000-01-01', 's')).item()
Out[5]: datetime.datetime(2000, 1, 1, 0, 0)
In [6]: np.array(np.datetime64('2000-01-01', 'ns')).item()
Out[6]: 946684800000000000
The underlying issue is that not all datetime64 types can be represented in datetime.datetime
objects, which have fixed us precision.
We should either:
- Raise
TypeError
in all cases when calling.item()
on a datetime64[ns] array. - Convert to
datetime.datetime
if it can be done safely, raiseValueError
otherwise.
My preference is for 2.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (9 by maintainers)
Top Results From Across the Web
Error when converting numpy.datetime64 to int
I think this behavior ensures that the conversion to a serial date/time is unambiguous - if the unit of the input datetime is...
Read more >What's New — xarray 0.10.2 documentation
The minor release includes a number of bug-fixes and enhancements, ... DataArrayRolling() object now supports construct() method that returns a view of the ......
Read more >What's New — pandas 0.17.0 documentation
Timedelta.total_seconds() now returns Timedelta duration to ns precision (previously ... Bug in isnull when applied to 0-dimensional object arrays (GH7176) ...
Read more >What's New — pandas 0.19.2 documentation
NaT; PeriodIndex.values now returns array of Period object ... .to_stata() and StataWriter will automatically convert datetime64[ns] columns to Stata format ...
Read more >Datetimes and Timedeltas — NumPy v1.24 Manual
Starting in NumPy 1.7, there are core array data types which natively ... of two datetime values, an operation which produces a number...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fair, but apparently
.astype
(which I do need to use) follows.item
-semantics. And how far I’d personally turn the dial of “not casting” shouldn’t change the fact that the proposal makes sense for datetimes, no?Also fair, but probably better discussed in #12550.
In this sense, it’s good to have separate issues, because even if
astype(object)
were fixed to follow different semantics, the problem of the OP here would persist (so not exactly duplicate).