Python xarray CFTimeIndex.to_datetimeindex越界纳秒时间戳
最近,我尝试使用Python xarray CFTimeIndex.to_datetimeindex越界纳秒时间戳,python,pandas,datetime,python-xarray,Python,Pandas,Datetime,Python Xarray,最近,我尝试使用xarray.CFTimeIndex.to_datetimeindex函数将HadCM3 NetCDF时间对象中的月近地表气温从过去1000次实验(从公元850年到公元1850年)更改为datetime对象: xr.CFTimeIndex.to_datetimeindex(tas['time']) 我得到了这个信息: OutOfBoundsDatetime Traceback (most recent call last) ~/ana
xarray.CFTimeIndex.to_datetimeindex
函数将HadCM3 NetCDF时间对象中的月近地表气温从过去1000次实验(从公元850年到公元1850年)更改为datetime对象:
xr.CFTimeIndex.to_datetimeindex(tas['time'])
我得到了这个信息:
OutOfBoundsDatetime Traceback (most recent call last)
~/anaconda3/envs/tutorial_xarray/lib/python3.7/site-packages/xarray/coding/times.py in cftime_to_nptime(times)
296 dt = pd.Timestamp(
--> 297 t.year, t.month, t.day, t.hour, t.minute, t.second, t.microsecond
298 )
pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.__new__()
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_to_tsobject()
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject()
pandas/_libs/tslibs/np_datetime.pyx in pandas._libs.tslibs.np_datetime.check_dts_bounds()
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 850-01-16 00:00:00
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-30-7ed217c97bf1> in <module>
----> 1 xr.CFTimeIndex.to_datetimeindex(tas['time'])
~/anaconda3/envs/tutorial_xarray/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in to_datetimeindex(self, unsafe)
486 DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[ns]', freq=None)
487 """
--> 488 nptimes = cftime_to_nptime(self)
489 calendar = infer_calendar_name(self)
490 if calendar not in _STANDARD_CALENDARS and not unsafe:
~/anaconda3/envs/tutorial_xarray/lib/python3.7/site-packages/xarray/coding/times.py in cftime_to_nptime(times)
300 raise ValueError(
301 "Cannot convert date {} to a date in the "
--> 302 "standard calendar. Reason: {}.".format(t, e)
303 )
304 new[i] = np.datetime64(dt)
ValueError: Cannot convert date 0850-01-16 00:00:00 to a date in the standard calendar. Reason: Out of bounds nanosecond timestamp: 850-01-16 00:00:00.
基于,我知道可以用64位整数表示的时间跨度在pandas()中被限制为大约584年
有人能帮我把这个数据转换成datetime对象吗
谢谢请问您为什么有兴趣将此索引转换为DatetimeIndex?这个限制是使用CFTimeIndex的主要动机之一。@spencerclark啊,我愚蠢的错误……事实上,我想用那个
数据数组
做简单的绘图,但它不适用于CFTimeIndex,所以我将它转换为pandas datetime对象。但是,在我导入nc时间轴模块后,它工作得非常好。因此,我不再需要将其转换为pandas的datetime对象。谢谢你的评论。一切都好@sandyherho;很高兴你把问题解决了@spencerkclark那么,如果你想从多个模型中得到一个集合,而其他模型都有标准的时间索引,你该怎么办?@janchytry确实有跨模型协调日期类型的用例。根据情况,将所有内容转换为cftime日期类型或将所有内容转换为datetime64是有意义的;我必须看更多的细节(最好单独问一个问题)。
<xarray.DataArray 'tas' (time: 12012, lat: 73, lon: 96)>
[84180096 values with dtype=float32]
Coordinates:
* time (time) object 0850-01-16 00:00:00 ... 1850-12-16 00:00:00
* lat (lat) float64 -90.0 -87.5 -85.0 -82.5 -80.0 ... 82.5 85.0 87.5 90.0
* lon (lon) float64 0.0 3.75 7.5 11.25 15.0 ... 345.0 348.8 352.5 356.2
height float64 ...
Attributes:
standard_name: air_temperature
long_name: Near-Surface Air Temperature
units: K
original_name: mo: m01s03i236
cell_methods: time: mean
cell_measures: area: areacella
history: 2013-03-06T13:59:55Z altered by CMOR: Treated scalar d...
associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation...
$ pip freeze | grep xarray
xarray==0.15.0