Skip to content

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

Open
@shoyer

Description

@shoyer

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions