Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.item() on a DataArray with dtype='datetime64[ns]' returns int #3256

Open
IvoCrnkovic opened this issue Aug 23, 2019 · 4 comments
Open

.item() on a DataArray with dtype='datetime64[ns]' returns int #3256

IvoCrnkovic opened this issue Aug 23, 2019 · 4 comments

Comments

@IvoCrnkovic
Copy link

IvoCrnkovic commented Aug 23, 2019

MCVE Code Sample

import datetime
import xarray as xr

test_da = xr.DataArray(datetime.datetime(2019, 1, 1, 1, 1))

test_da
# <xarray.DataArray ()>
# array('2019-01-01T01:01:00.000000000', dtype='datetime64[ns]')

test_da.item()
# 1546304460000000000

Expected Output

I would think it would be nice to get a datetime out of the .item() call then the nanosecond representation.

Output of xr.show_versions()

When I call xr.show_versions() i get an error but im running xarray 0.12.3
@shoyer
Copy link
Member

shoyer commented Aug 24, 2019

This is basically a direct manifestation of this NumPy bug: numpy/numpy#12550

We could potentially had a workaround in xarray but it would be good to figure out the NumPy fix first, so we can be consistent.

@dopplershift
Copy link
Contributor

Well it'd be nice to have something, because what we have to do right now is:

times[0].values.astype('datetime64[ms]').astype('O')

which is an abomination and terrible for teaching.

@jthielen
Copy link
Contributor

Would it make sense to add something to the dt accessor, such as_datetime? @dopplershift's example would still be somewhat verbose, but it seems more sensible:

times[0].dt.as_datetime().item()

@dopplershift
Copy link
Contributor

Yeah, it's not much shorter, but it's a much easier concept for new users to grasp, IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants