Skip to content

DataArray constructor still coerces to np.datetime64[ns], not cftime in 0.11.0 #2587

Closed
@Huite

Description

@Huite

Code Sample

import xarray as xr
import numpy as np
from datetime import datetime

time = [np.datetime64(datetime.strptime("10000101", "%Y%m%d"))]
print(time[0])
print(np.dtype(time[0]))

da = xr.DataArray(time, ("time",), {"time":time})
print(da)

Results in:

1000-01-01T00:00:00.000000
datetime64[us]

<xarray.DataArray (time: 1)>
array(['2169-02-08T23:09:07.419103232'], dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2169-02-08T23:09:07.419103232

Problem description

I was happy to see cftime as default in the release notes for 0.11.0:

Xarray will now always use cftime.datetime objects, rather than by default trying to coerce them into np.datetime64[ns] objects. A CFTimeIndex will be used for indexing along time coordinates in these cases.

However, it seems that the DataArray constructor does not use cftime (yet?), and coerces to np.datetime64[ns] here:

if isinstance(data, np.ndarray):
if data.dtype.kind == 'O':
data = _possibly_convert_objects(data)
elif data.dtype.kind == 'M':
data = np.asarray(data, 'datetime64[ns]')
elif data.dtype.kind == 'm':
data = np.asarray(data, 'timedelta64[ns]')

Expected Output

I think I'd expect cftime.datetime in this case as well. Some coercion happens anyway as pandas timestamps are turned into np.datetime64[ns].

(But perhaps this was already on your radar, and am I just a little too eager!)

Output of xr.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

xarray: 0.11.0
pandas: 0.23.3
numpy: 1.15.3
scipy: 1.1.0
netCDF4: 1.3.1
h5netcdf: 0.6.1
h5py: 2.8.0
Nio: None
zarr: None
cftime: 1.0.0
PseudonetCDF: None
rasterio: 1.0.0
iris: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.19.2
distributed: 1.23.2
matplotlib: 2.2.2
cartopy: 0.16.0
seaborn: 0.9.0
setuptools: 40.5.0
pip: 18.1
conda: None
pytest: 3.6.3
IPython: 6.4.0
sphinx: 1.7.5

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