Transform a time-indexed Dataset or DataArray to one that uses another calendar. This function only converts the individual timestamps; it does not modify any data except in dropping invalid/surplus dates, or inserting values for missing dates. If the source and target calendars are bo
(
obj,
calendar,
dim="time",
align_on=None,
missing=None,
use_cftime=None,
)
| 30 | |
| 31 | |
| 32 | def convert_calendar( |
| 33 | obj, |
| 34 | calendar, |
| 35 | dim="time", |
| 36 | align_on=None, |
| 37 | missing=None, |
| 38 | use_cftime=None, |
| 39 | ): |
| 40 | """Transform a time-indexed Dataset or DataArray to one that uses another calendar. |
| 41 | |
| 42 | This function only converts the individual timestamps; it does not modify any |
| 43 | data except in dropping invalid/surplus dates, or inserting values for missing dates. |
| 44 | |
| 45 | If the source and target calendars are both from a standard type, only the |
| 46 | type of the time array is modified. When converting to a calendar with a |
| 47 | leap year from to a calendar without a leap year, the 29th of February will |
| 48 | be removed from the array. In the other direction the 29th of February will |
| 49 | be missing in the output, unless `missing` is specified, in which case that |
| 50 | value is inserted. For conversions involving the `360_day` calendar, see Notes. |
| 51 | |
| 52 | This method is safe to use with sub-daily data as it doesn't touch the time |
| 53 | part of the timestamps. |
| 54 | |
| 55 | Parameters |
| 56 | ---------- |
| 57 | obj : DataArray or Dataset |
| 58 | Input DataArray or Dataset with a time coordinate of a valid dtype |
| 59 | (:py:class:`numpy.datetime64` or :py:class:`cftime.datetime`). |
| 60 | calendar : str |
| 61 | The target calendar name. |
| 62 | dim : str |
| 63 | Name of the time coordinate in the input DataArray or Dataset. |
| 64 | align_on : {None, 'date', 'year', 'random'} |
| 65 | Must be specified when either the source or target is a `"360_day"` |
| 66 | calendar; ignored otherwise. See Notes. |
| 67 | missing : any, optional |
| 68 | By default, i.e. if the value is None, this method will simply attempt |
| 69 | to convert the dates in the source calendar to the same dates in the |
| 70 | target calendar, and drop any of those that are not possible to |
| 71 | represent. If a value is provided, a new time coordinate will be |
| 72 | created in the target calendar with the same frequency as the original |
| 73 | time coordinate; for any dates that are not present in the source, the |
| 74 | data will be filled with this value. Note that using this mode requires |
| 75 | that the source data have an inferable frequency; for more information |
| 76 | see :py:func:`xarray.infer_freq`. For certain frequency, source, and |
| 77 | target calendar combinations, this could result in many missing values, see notes. |
| 78 | use_cftime : bool, optional |
| 79 | Whether to use cftime objects in the output, only used if `calendar` is |
| 80 | one of {"proleptic_gregorian", "gregorian" or "standard"}. |
| 81 | If True, the new time axis uses cftime objects. |
| 82 | If None (default), it uses :py:class:`numpy.datetime64` values if the date |
| 83 | range permits it, and :py:class:`cftime.datetime` objects if not. |
| 84 | If False, it uses :py:class:`numpy.datetime64` or fails. |
| 85 | |
| 86 | Returns |
| 87 | ------- |
| 88 | Copy of source with the time coordinate converted to the target calendar. |
| 89 | If `missing` was None (default), invalid dates in the new calendar are |
searching dependent graphs…