You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Relax nanosecond datetime restriction in CF time decoding (#9618)
Co-authored-by: Stephan Hoyer <[email protected]>
Co-authored-by: Deepak Cherian <[email protected]>
Co-authored-by: Spencer Clark <[email protected]>
Co-authored-by: Spencer Clark <[email protected]>
Co-authored-by: Stephan Hoyer <[email protected]>
Co-authored-by: Spencer Clark <[email protected]>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix scalar handling for timedelta based indexer
* remove stale error message and "ignore:Converting non-default" in testsuite
* add per review suggestions
* add/remove todo
* rename timeunit -> format
* return "ns" resolution per default for timedeltas, if not specified
* Be specific on types/dtpyes
* add comment
* add suggestions from code review
* fix docs
* fix test which isn't run for numpy2 atm
* add notes on to_datetime section, update examples showing usage of 'as_unit'
* use np.timedelta64 for to_timedelta example, update as_unit example, update note
* remove note
* Apply suggestions from code review
Co-authored-by: Deepak Cherian <[email protected]>
* refactor timedelta decoding to _numbers_to_timedelta and res-use it within decode_cf_timedelta
* fix conventions test, add todo
* run times through pd.Timestamp to catch possible overflows
* fix tests for cftime_to_nptime
* fix cftime_to_nptime in cftimeindex
* introduce pd.Timestamp instance check
* warn if out-of-bound datetimes are encoded with standard calendar, fall back to cftime encoding, add fix for cftime issue where python datetimes are not encoded correctly with date2num.
* fix time-coding.rst, add reference to time-series.rst.
* try to fix typing, ignore one
* try to fix docs
* revert doc-changes
* Add a non-ns test for polyval, polyfit
* more doc cosmetics
* add whats-new.rst entry
* add/fix coder docstring
* add xr.date_range example as suggested per review
* Apply suggestions from code review
Co-authored-by: Spencer Clark <[email protected]>
* Implement `time_unit` option for `decode_cf_timedelta` (#3)
* Fix timedelta encoding overflow issue; always decode to ns resolution
* Implement time_unit for decode_cf_timedelta
* Reduce diff
* fix typing
* use nanmin/nanmax, catch numpy RuntimeWarnings
* Apply suggestions from code review
Co-authored-by: Kai Mühlbauer <[email protected]>
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Stephan Hoyer <[email protected]>
Co-authored-by: Deepak Cherian <[email protected]>
Co-authored-by: Spencer Clark <[email protected]>
Co-authored-by: Deepak Cherian <[email protected]>
One unfortunate limitation of using ``datetime64[ns]`` is that it limits the
72
-
native representation of dates to those that fall between the years 1678 and
73
-
2262. When a netCDF file contains dates outside of these bounds, dates will be
74
-
returned as arrays of :py:class:`cftime.datetime` objects and a :py:class:`~xarray.CFTimeIndex`
75
-
will be used for indexing. :py:class:`~xarray.CFTimeIndex` enables a subset of
76
-
the indexing functionality of a :py:class:`pandas.DatetimeIndex` and is only
77
-
fully compatible with the standalone version of ``cftime`` (not the version
78
-
packaged with earlier versions ``netCDF4``). See :ref:`CFTimeIndex` for more
79
-
information.
96
+
From xarray 2025.01.2 the resolution of the dates can be one of ``"s"``, ``"ms"``, ``"us"`` or ``"ns"``. One limitation of using ``datetime64[ns]`` is that it limits the native representation of dates to those that fall between the years 1678 and 2262, which gets increased significantly with lower resolutions. When a store contains dates outside of these bounds (or dates < `1582-10-15`_ with a Gregorian, also known as standard, calendar), dates will be returned as arrays of :py:class:`cftime.datetime` objects and a :py:class:`~xarray.CFTimeIndex` will be used for indexing.
97
+
:py:class:`~xarray.CFTimeIndex` enables most of the indexing functionality of a :py:class:`pandas.DatetimeIndex`.
Copy file name to clipboardExpand all lines: doc/user-guide/weather-climate.rst
+13-16
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Weather and climate data
10
10
11
11
import xarray as xr
12
12
13
-
Xarray can leverage metadata that follows the `Climate and Forecast (CF) conventions`_ if present. Examples include :ref:`automatic labelling of plots<plotting>` with descriptive names and units if proper metadata is present and support for non-standard calendars used in climate science through the ``cftime`` module(Explained in the :ref:`CFTimeIndex` section). There are also a number of :ref:`geosciences-focused projects that build on xarray<ecosystem>`.
13
+
Xarray can leverage metadata that follows the `Climate and Forecast (CF) conventions`_ if present. Examples include :ref:`automatic labelling of plots<plotting>` with descriptive names and units if proper metadata is present and support for non-standard calendars used in climate science through the ``cftime`` module (explained in the :ref:`CFTimeIndex` section). There are also a number of :ref:`geosciences-focused projects that build on xarray<ecosystem>`.
14
14
15
15
.. _Climate and Forecast (CF) conventions: https://cfconventions.org
Through the standalone ``cftime`` library and a custom subclass of
64
64
:py:class:`pandas.Index`, xarray supports a subset of the indexing
65
65
functionality enabled through the standard :py:class:`pandas.DatetimeIndex` for
66
66
dates from non-standard calendars commonly used in climate science or dates
67
-
using a standard calendar, but outside the `nanosecond-precision range`_
68
-
(approximately between years 1678 and 2262).
67
+
using a standard calendar, but outside the `precision range`_ and dates prior to `1582-10-15`_.
69
68
70
69
.. note::
71
70
@@ -75,18 +74,14 @@ using a standard calendar, but outside the `nanosecond-precision range`_
75
74
any of the following are true:
76
75
77
76
- The dates are from a non-standard calendar
78
-
- Any dates are outside the nanosecond-precision range.
77
+
- Any dates are outside the nanosecond-precision range (prior xarray version 2025.01.2)
78
+
- Any dates are outside the time span limited by the resolution (from xarray version 2025.01.2)
79
79
80
80
Otherwise pandas-compatible dates from a standard calendar will be
81
-
represented with the ``np.datetime64[ns]`` data type, enabling the use of a
82
-
:py:class:`pandas.DatetimeIndex` or arrays with dtype ``np.datetime64[ns]``
83
-
and their full set of associated features.
81
+
represented with the ``np.datetime64[unit]`` data type (where unit can be one of ``"s"``, ``"ms"``, ``"us"``, ``"ns"``), enabling the use of a :py:class:`pandas.DatetimeIndex` or arrays with dtype ``np.datetime64[unit]`` and their full set of associated features.
84
82
85
83
As of pandas version 2.0.0, pandas supports non-nanosecond precision datetime
86
-
values. For the time being, xarray still automatically casts datetime values
87
-
to nanosecond-precision for backwards compatibility with older pandas
88
-
versions; however, this is something we would like to relax going forward.
89
-
See :issue:`7493` for more discussion.
84
+
values. From xarray version 2025.01.2 on, non-nanosecond precision datetime values are also supported in xarray (this can be parameterized via :py:class:`~xarray.coders.CFDatetimeCoder` and ``decode_times`` kwarg). See also :ref:`internals.timecoding`.
90
85
91
86
For example, you can create a DataArray indexed by a time
92
87
coordinate with dates from a no-leap calendar and a
@@ -115,7 +110,7 @@ instance, we can create the same dates and DataArray we created above using:
115
110
Mirroring pandas' method with the same name, :py:meth:`~xarray.infer_freq` allows one to
116
111
infer the sampling frequency of a :py:class:`~xarray.CFTimeIndex` or a 1-D
117
112
:py:class:`~xarray.DataArray` containing cftime objects. It also works transparently with
118
-
``np.datetime64[ns]`` and ``np.timedelta64[ns]`` data.
113
+
``np.datetime64`` and ``np.timedelta64`` data (with "s", "ms", "us" or "ns" resolution).
119
114
120
115
.. ipython:: python
121
116
@@ -137,7 +132,9 @@ Conversion between non-standard calendar and to/from pandas DatetimeIndexes is
137
132
facilitated with the :py:meth:`xarray.Dataset.convert_calendar` method (also available as
138
133
:py:meth:`xarray.DataArray.convert_calendar`). Here, like elsewhere in xarray, the ``use_cftime``
139
134
argument controls which datetime backend is used in the output. The default (``None``) is to
140
-
use ``pandas`` when possible, i.e. when the calendar is standard and dates are within 1678 and 2262.
135
+
use ``pandas`` when possible, i.e. when the calendar is ``standard``/``gregorian`` and dates starting with `1582-10-15`_. There is no such restriction when converting to a ``proleptic_gregorian`` calendar.
In a future release the current default of ``'ns'`` resolution on decoding will
49
+
eventually be deprecated.
50
+
22
51
New Features
23
52
~~~~~~~~~~~~
24
-
53
+
- Relax nanosecond datetime restriction in CF time decoding (:issue:`7493`, :pull:`9618`).
54
+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Spencer Clark <https://github.com/spencerkclark>`_.
25
55
26
56
Breaking changes
27
57
~~~~~~~~~~~~~~~~
@@ -37,7 +67,8 @@ Bug fixes
37
67
38
68
Documentation
39
69
~~~~~~~~~~~~~
40
-
70
+
- A chapter on :ref:`internals.timecoding` is added to the internal section (:pull:`9618`).
71
+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
0 commit comments