0

I recently tried to change monthly near-surface air temperature in HadCM3 NetCDF time-object from past1000 experiment (from 850 AD to 1850 AD) to datetime object using xarray.CFTimeIndex.to_datetimeindex function:

xr.CFTimeIndex.to_datetimeindex(tas['time'])

and got this message:

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.

This is my tas object:

<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...

and I'm currently using:

$ pip freeze | grep xarray
xarray==0.15.0

Based on the the previous question, I know that he timespan that can be represented using a 64-bit integer is limited to approximately 584 years in pandas (of which this function built on-top).

Could anyone help me to convert this data to datetime object?

Thanks

  • May I ask why you are interested in converting this index to a DatetimeIndex? This limitation is one of the primary motivations for using a CFTimeIndex. – spencerkclark Feb 13 '20 at 11:02
  • @spencerclark Ah, my silly mistake ...Actually, I wanted to make simple plot using that `DataArray`, and it didn't work with CFTimeIndex, so I converted it to pandas datetime object. But, after I import nc-time-axis module, it works perfectly fine. So, I don't need to convert it to pandas' datetime object anymore. Thanks btw for your comment. – kartikasenja Feb 14 '20 at 21:20
  • All good @sandyherho; glad you got it sorted out! – spencerkclark Feb 16 '20 at 01:40
  • @spencerkclark So what do you do if you want to make an ensemble mean from multiple models and the other models have standard time indices? – janchytry Feb 13 '21 at 17:11
  • @janchytry there are indeed use cases for harmonizing date types across models. Depending on the situation it could make sense to convert everything to cftime date types or everything to datetime64; I'd have to see more details (it might be good to ask a separate question). – spencerkclark Feb 13 '21 at 17:21
  • @spencerkclark I opened a new question/discussion, please see https://stackoverflow.com/questions/66188904/ways-to-resample-non-standard-cftimeindex-calendars-360-day-no-leap-year-with – janchytry Feb 13 '21 at 19:12

0 Answers0