-
-
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
to_zarr(append_dim="time")
appends incorrect datetimes
#5969
Labels
Comments
Sorry this didn't get traction from a couple of years ago. The example is high-quality! But is there a single copy-pastable example we could have to assess whether it's still a problem? |
max-sixty
added
the
needs mcve
https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports
label
Oct 14, 2023
Oh this is quite bad: import pandas as pd
import xarray as xr
times = pd.date_range("2000-01-01 00:35", periods=8, freq="6h")
da = xr.DataArray(coords=[times], dims=["time"])
ds = da.to_dataset(name="foo")
ZARR_PATH = "test.zarr"
ds.isel(time=[0]).to_zarr(ZARR_PATH, mode="w")
ds.isel(time=slice(1, None)).to_zarr(ZARR_PATH, append_dim="time")
ds_loaded = xr.open_dataset(ZARR_PATH, engine="zarr")
print(ds_loaded.time) we get
instead of the expected
We do get a warning that makes it clear what is happening though
So we are appending values that were encoded differently |
dcherian
removed
the
needs mcve
https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports
label
May 3, 2024
Closing as dupe of #3942 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
If you create a Zarr with a single timestep and then append to the
time
dimension of that Zarr in subsequent writes then the appended timestamps are likely to be wrong. This only seems to happen if thetime
dimension isdatetime64
.Minimal Complete Verifiable Example
Create a really simple
Dataset
:Write just the first timestep to a new Zarr store:
So far, so good!
Now things get weird... let's append the remainder of
ds
to the Zarr store:This throws a warning, which is probably relevant:
What happened
Let's load the Zarr and print the contents on the
time
coord:(I've removed the seconds and milliseconds to make it a bit easier to read)
The first and fifth time coords (2000-01-01T00:35 and 2000-01-02T00:35) are correct. None of the others are correct!
The encoding is not appropriate (see #3942)... notice that the
units
isdays since...
, which clearly can't represent sub-day resolution:What you expected to happen
The correct
time
coords are:Anything else we need to know?
There are three workarounds that I'm aware of:
time
coords to Unix epoch, represented as ints.int64
when writing netcdf or zarr #3942 (comment)). For example:Related issues
It's possible that the root cause of this issue is #3942.
And I think #3379 is another symptom of this issue.
Environment
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:46)
[GCC 9.4.0]
python-bits: 64
OS: Linux
OS-release: 5.13.0-21-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1
xarray: 0.20.1
pandas: 1.3.4
numpy: 1.21.4
scipy: 1.7.2
netCDF4: 1.5.8
pydap: None
h5netcdf: 0.11.0
h5py: 3.4.0
Nio: None
zarr: 2.10.1
cftime: 1.5.1.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.2.8
cfgrib: 0.9.9.1
iris: None
bottleneck: 1.3.2
dask: 2021.10.0
distributed: None
matplotlib: 3.4.3
cartopy: None
seaborn: None
numbagg: None
fsspec: 2021.11.0
cupy: None
pint: None
sparse: None
setuptools: 58.5.3
pip: 21.3.1
conda: None
pytest: 6.2.5
IPython: 7.29.0
sphinx: None
The text was updated successfully, but these errors were encountered: