-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Millisecond precision is lost on datetime64 during IO roundtrip #4045
Comments
This has something to do with the time values at some point being a float: >>> import numpy as np
>>> np.datetime64("2017-02-22T16:24:10.586000000").astype("float64").astype(np.dtype('<M8[ns]'))
numpy.datetime64('2017-02-22T16:24:10.585999872') It looks like this is happening somewhere in the cftime library. |
Thanks for the report @half-adder. This indeed is related to times being encoded as floats, but actually is not cftime-related (the times here not being encoded using cftime; we only use cftime for non-standard calendars and out of nanosecond-resolution bounds dates). Here's a minimal working example that illustrates the issue with the current logic in
In principle, we should be able to represent the difference between this date and the reference date in an integer amount of microseconds, but timedelta division produces a float. We currently try to cast these floats to integers when possible, but that's not always safe to do, e.g. in the case above. It would be great to make roundtripping times -- particularly standard calendar datetimes like these -- more robust. It's possible we could now leverage floor division (i.e.
These precision issues can be tricky, however, so we'd need to think things through carefully. Even if we fixed this on the encoding side, things are converted to floats during decoding, so we'd need to make a change there too. |
Just stumbled upon this as well. Internally, Simply telling it to Is there a way out of this, other than not using This is a huge issue, as anyone using nanosecond-precision timestamps with xarray would unknowingly and silently read wrong data after deserializing. |
The short answer is that CF conventions allow for dates to be encoded with floating point values, so we encounter that in data that xarray ingests from other sources (i.e. files that were not even produced with Python, let alone xarray). If we didn't have to worry about roundtripping files that followed those conventions, I agree we would just encode everything with nanosecond units as
Yes, I can see why this would be quite frustrating. In principle we should be able to handle this (contributions are welcome); it just has not been a priority up to this point. In my experience xarray's current encoding and decoding methods for standard calendar times work well up to at least second precision. |
Can we use the |
I don't mind contributing but not knowing the netcdf stuff inside out I'm not sure I have a good vision on what's the proper way to do it. My use case is very simple - I have an in-memory xr.Dataset that I want to save() and then load() without losses. Should it just be an Maybe this is not the proper way to do it - ideas welcome (there's also an open PR - #4400 - mind checking that out?) |
I think a lot of logic needs to be reshuffled, because as of right now it will complain "you can't store a float64 in int64" or something along those lines, when trying to do it with a nanosecond timestamp. |
I would look here: Lines 440 to 474 in 255bc8e
|
@half-adder I've verified that #4684 fixes your initial issue. Note, however, that outside of the time you referenced, your Dataset contained times that required nanosecond precision, e.g.: >>> data.time.isel(animal=0, timepoint=0, pair=-1, wavelength=0)
<xarray.DataArray 'time' ()>
array('2017-02-22T16:24:14.722999999', dtype='datetime64[ns]')
Coordinates:
wavelength <U3 '410'
strain object 'HD233'
stage_x float64 1.64e+04
stage_y float64 -429.0
stage_z float64 2.155e+04
bin_x float64 4.0
bin_y float64 4.0
exposure float64 90.0
mvmt-anterior uint8 0
mvmt-posterior uint8 0
mvmt-sides_of_tip uint8 0
mvmt-tip uint8 0
experiment_id object '2017_02_22-HD233_SAY47'
time datetime64[ns] 2017-02-22T16:24:14.722999999
animal_ uint64 0 So in order for things to be round-tripped accurately you will need to override the original units in the dataset with nanoseconds instead of microseconds. This was not possible before, but now is with #4684. >>> data.time.encoding["units"] = "nanoseconds since 1900-01-01" With #4684 you could also just simply delete the original units, and xarray will now automatically choose the appropriate units so that the datetimes can be serialized with >>> del data.time.encoding["units"] |
I have millisecond-resolution time data as a coordinate on a DataArray. That data loses precision when round-tripping through disk.
MCVE Code Sample
bug_data.p.zip
Unzip the data. It will result in a pickle file.
output:
Expected Output
Problem Description
As you can see, I lose millisecond precision in this data. (The same happens when I use millisecond in the encoding).
Versions
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.6 | packaged by conda-forge | (default, Jan 7 2020, 22:05:27)
[Clang 9.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 19.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.3
xarray: 0.15.1
pandas: 1.0.1
numpy: 1.18.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: 0.8.0
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.11.0
distributed: 2.14.0
matplotlib: 3.1.3
cartopy: None
seaborn: 0.10.0
numbagg: None
setuptools: 45.2.0.post20200209
pip: 20.0.2
conda: None
pytest: 5.3.5
IPython: 7.12.0
sphinx: 2.4.3
The text was updated successfully, but these errors were encountered: