Python 3.x xarray:保存后的错误时间

Python 3.x xarray:保存后的错误时间,python-3.x,python-xarray,netcdf4,Python 3.x,Python Xarray,Netcdf4,在使用大值保存时间时,有人可以重现xarray的这种行为吗?我有点不知道这里发生了什么 编辑:如果“时间”的数值超过某个阈值,则xarray似乎做错了什么。请注意,这只发生在“自”之后的天内,而不发生在“自”之后的秒内 我正在使用Python3和xarray版本0.10.7 将numpy导入为np 将xarray作为xr导入 #打印('xarray version:{}'。格式(xr.\uuuuuu version\uuuuuuu)) ds=xr.Dataset(坐标={'time':( “时间

在使用大值保存时间时,有人可以重现xarray的这种行为吗?我有点不知道这里发生了什么

编辑:如果“时间”的数值超过某个阈值,则xarray似乎做错了什么。请注意,这只发生在“自”之后的天内,而不发生在“自”之后的秒内

我正在使用Python3和xarray版本0.10.7

将numpy导入为np
将xarray作为xr导入
#打印('xarray version:{}'。格式(xr.\uuuuuu version\uuuuuuu))
ds=xr.Dataset(坐标={'time':(
“时间”,
np.arange(106300.5106665.5+5*365,365),
{'units':'1800-01-01 00:00:00'之后的天数})
#打印(ds.时间)
ds=xr.decode\u cf(ds)
#打印(ds.时间)
ds.to_netcdf('./test.nc')
ds=xr.open_数据集('./test.nc',decode_cf=False)
打印(ds.时间)
输出:


是的,我可以复制这个。这可能被认为是xarray中的一个bug;你可以考虑提出一个问题。

保存文件时,在后台,xarray将获取解码的日期,并将其转换为自参考日期起的时间增量。问题在于,示例数据集中的日期跨越了比提供的参考日期(1800-01-01)晚292年的界限


这里我特意选择了自1970-01-01以来的
“天”
,因为这就是
np.datetime64
对象在NumPy中的中心位置。

你是指表示浮动的负时间吗?
test.nc
文件的内容是什么?这已通过检查@skc在版本0.11.0中修复!我进一步调查了一下,并提交了一份报告。
<xarray.DataArray 'time' (time: 6)>
array([ 106300.5     ,  106665.5     , -106473.482335, -106108.482335,
       -105743.482335, -105378.482335])
Coordinates:
  * time     (time) float64 1.063e+05 1.067e+05 -1.065e+05 -1.061e+05 ...
Attributes:
    _FillValue:  nan
    units:       days since 1800-01-01
    calendar:    proleptic_gregorian
netcdf test {
dimensions:
    time = 6 ;
variables:
    double time(time) ;
        time:_FillValue = NaN ;
        time:units = "days since 1800-01-01" ;
        time:calendar = "proleptic_gregorian" ;

// global attributes:
        :_NCProperties = "version=1|netcdflibversion=4.4.1.1|hdf5libversion=1.10.1" ;
data:

 time = 106300.5, 106665.5, -106473.482334601, -106108.482334601, 
    -105743.482334601, -105378.482334601 ;
}
In [1]: import numpy as np

In [2]: import xarray as xr

In [3]: ds = xr.Dataset(coords={'time': (
   ...:     'time',
   ...:     np.arange(106300.5, 106665.5+5*365, 365),
   ...:     {'units': 'days since 1800-01-01 00:00:00'})})
   ...:

In [4]: ds = xr.decode_cf(ds)

In [5]: ds.time
Out[5]:
<xarray.DataArray 'time' (time: 6)>
array(['2091-01-15T12:00:00.000000000', '2092-01-15T12:00:00.000000000',
       '2093-01-14T12:00:00.000000000', '2094-01-14T12:00:00.000000000',
       '2095-01-14T12:00:00.000000000', '2096-01-14T12:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2091-01-15T12:00:00 2092-01-15T12:00:00 ...

In [6]: ds.to_netcdf('so.nc')

In [7]: xr.open_dataset('so.nc', decode_times=False).time
so.nc
Out[7]:
<xarray.DataArray 'time' (time: 6)>
array([ 106300.5     ,  106665.5     , -106473.482335, -106108.482335,
       -105743.482335, -105378.482335])
Coordinates:
  * time     (time) float64 1.063e+05 1.067e+05 -1.065e+05 -1.061e+05 ...
Attributes:
    units:     days since 1800-01-01
    calendar:  proleptic_gregorian
In [8]: ds.time.encoding['units'] = 'days since 1970-01-01'

In [9]: ds.to_netcdf('so-workaround.nc')

In [10]: xr.open_dataset('so-workaround.nc', decode_times=False).time
Out[10]:
<xarray.DataArray 'time' (time: 6)>
array([44209.5, 44574.5, 44939.5, 45304.5, 45669.5, 46034.5])
Coordinates:
  * time     (time) float64 4.421e+04 4.457e+04 4.494e+04 4.53e+04 4.567e+04 ...
Attributes:
    units:     days since 1970-01-01
    calendar:  proleptic_gregorian