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.

BUG: .item() on a 0-dimensional datetime64[ns] array yields an integer

See original GitHub issue

This 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:

  1. Raise TypeError in all cases when calling .item() on a datetime64[ns] array.
  2. Convert to datetime.datetime if it can be done safely, raise ValueError otherwise.

My preference is for 2.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
h-vetinaricommented, Dec 2, 2019

I’m starting to think the answer is “then don’t use item”.

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?

0reactions
h-vetinaricommented, Dec 2, 2019

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).

Read more comments on GitHub >

github_iconTop 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 >

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