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

Time encoding error associated with cftime > 1.4.0 #4870

Closed
spencerkclark opened this issue Feb 6, 2021 · 0 comments · Fixed by #4871
Closed

Time encoding error associated with cftime > 1.4.0 #4870

spencerkclark opened this issue Feb 6, 2021 · 0 comments · Fixed by #4871

Comments

@spencerkclark
Copy link
Member

As of cftime > 1.4.0, the return type of cftime.date2num can either be an integer or float. An integer dtype is used if the times can all be encoded exactly with the provided units; otherwise a float dtype is used. This causes problems in our current encoding pipeline, because we call cftime.date2num on dates one at a time through np.vectorize, and np.vectorize infers the type of the full returned array based on the result of the first function evaluation. If the first result is an integer, then the full array will be assumed to have an integer dtype, and any values that should be floats are cast as integers.

What happened:

In [1]: import cftime; import numpy as np; import xarray as xr

In [2]: times = np.array([cftime.DatetimeGregorian(2000, 1, 1), cftime.DatetimeGregorian(2000, 1, 1, 1)])

In [3]: xr.coding.times._encode_datetime_with_cftime(times, "days since 2000-01-01", calendar="gregorian")
Out[3]: array([0, 0])

What you expected to happen:

In [3]: xr.coding.times._encode_datetime_with_cftime(times, "days since 2000-01-01", calendar="gregorian")
Out[3]: array([0.        , 0.04166667])

A solution here would be to encode the times with a list comprehension instead, and cast the final result to an array, in which case NumPy infers the dtype in a more sensible way.

Environment:

Output of xr.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 14:38:56)
[Clang 4.0.1 (tags/RELEASE_401/final)]
python-bits: 64
OS: Darwin
OS-release: 20.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.6.2

xarray: 0.16.2.dev175+g8cc34cb4.d20210201
pandas: 1.1.3
numpy: 1.19.1
scipy: 1.2.1
netCDF4: 1.5.1.2
pydap: installed
h5netcdf: 0.7.4
h5py: 2.9.0
Nio: None
zarr: 2.3.2
cftime: 1.4.1
nc_time_axis: 1.1.1.dev5+g531dd0d
PseudoNetCDF: None
rasterio: 1.0.25
cfgrib: 0.9.7.1
iris: None
bottleneck: 1.2.1
dask: 2.11.0
distributed: 2.11.0
matplotlib: 3.3.2
cartopy: None
seaborn: 0.9.0
numbagg: installed
pint: None
setuptools: 51.0.0.post20201207
pip: 19.2.2
conda: None
pytest: 5.0.1
IPython: 7.10.1
sphinx: 3.0.4
@spencerkclark spencerkclark changed the title Time encoding error associated with cftime > 1.4.0 Time encoding error associated with cftime >= 1.4.0 Feb 6, 2021
@spencerkclark spencerkclark changed the title Time encoding error associated with cftime >= 1.4.0 Time encoding error associated with cftime > 1.4.0 Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant