home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 402131750

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/2265#issuecomment-402131750 https://api.github.com/repos/pydata/xarray/issues/2265 402131750 MDEyOklzc3VlQ29tbWVudDQwMjEzMTc1MA== 6628425 2018-07-03T12:05:37Z 2018-07-03T12:05:37Z MEMBER

When writing datetime objects to disk, xarray encodes them following CF conventions (converting them to numerical values quantifying some units of time since a given reference date). This is to support other backends which do not support writing datetime64 objects directly to disk.

For accurate roundtripping of Datasets, xarray also includes logic to automatically decode datetimes stored following CF conventions. For that reason, instead of loading in the raw zarr store using zarr's open function, I recommend using xarray's open_zarr function, which will automatically decode the CF-encoded values to datetime64 objects.

See the following example: ``` In [1]: import numpy as np

In [2]: import xarray as xr

In [3]: da = xr.DataArray(np.datetime64('2000-01-01'), name='date')

In [4]: da Out[4]: <xarray.DataArray 'date' ()> array('2000-01-01T00:00:00.000000000', dtype='datetime64[ns]')

In [5]: da.to_dataset().to_zarr('example.zarr') Out[5]: <xarray.backends.zarr.ZarrStore at 0x1109ca190>

In [6]: ds = xr.open_zarr('example.zarr')

In [7]: ds.date Out[7]: <xarray.DataArray 'date' ()> array('2000-01-01T00:00:00.000000000', dtype='datetime64[ns]') Note if we open the zarr store directly, we'll find that the date was encoded with the integer `0` and appropriate units and calendar attributes: In [1]: import zarr

In [2]: z = zarr.open('example.zarr')

In [3]: z['date'][...] Out[3]: array(0)

In [4]: z['date'].attrs['units'] Out[4]: u'days since 2000-01-01 00:00:00'

In [5]: z['date'].attrs['calendar'] Out[5]: u'proleptic_gregorian' ``` Unlike xarray, zarr does not include logic for automatically decoding CF-encoded datetimes. I hope that helps.

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  337733183
Powered by Datasette · Queries took 0.574ms · About: xarray-datasette