🍾 Xarray is now 10 years old! 🎉

xarray.Dataset.convert_calendar

xarray.Dataset.convert_calendar#

Dataset.convert_calendar(calendar, dim='time', align_on=None, missing=None, use_cftime=None)[source]#

Convert the Dataset to another calendar.

Only converts the individual timestamps, does not modify any data except in dropping invalid/surplus dates or inserting missing dates.

If the source and target calendars are either no_leap, all_leap or a standard type, only the type of the time array is modified. When converting to a leap year from a non-leap year, the 29th of February is removed from the array. In the other direction the 29th of February will be missing in the output, unless missing is specified, in which case that value is inserted.

For conversions involving 360_day calendars, see Notes.

This method is safe to use with sub-daily data as it doesn’t touch the time part of the timestamps.

Parameters:
  • calendar (str) – The target calendar name.

  • dim (Hashable, default: "time") – Name of the time coordinate.

  • align_on ({None, 'date', 'year'}, optional) – Must be specified when either source or target is a 360_day calendar, ignored otherwise. See Notes.

  • missing (Any or None, optional) – By default, i.e. if the value is None, this method will simply attempt to convert the dates in the source calendar to the same dates in the target calendar, and drop any of those that are not possible to represent. If a value is provided, a new time coordinate will be created in the target calendar with the same frequency as the original time coordinate; for any dates that are not present in the source, the data will be filled with this value. Note that using this mode requires that the source data have an inferable frequency; for more information see xarray.infer_freq(). For certain frequency, source, and target calendar combinations, this could result in many missing values, see notes.

  • use_cftime (bool or None, optional) – Whether to use cftime objects in the output, only used if calendar is one of {“proleptic_gregorian”, “gregorian” or “standard”}. If True, the new time axis uses cftime objects. If None (default), it uses numpy.datetime64 values if the date range permits it, and cftime.datetime objects if not. If False, it uses numpy.datetime64 or fails.

Returns:

Dataset – Copy of the dataarray with the time coordinate converted to the target calendar. If ‘missing’ was None (default), invalid dates in the new calendar are dropped, but missing dates are not inserted. If missing was given, the new data is reindexed to have a time axis with the same frequency as the source, but in the new calendar; any missing datapoints are filled with missing.

Notes

Passing a value to missing is only usable if the source’s time coordinate as an inferable frequencies (see infer_freq()) and is only appropriate if the target coordinate, generated from this frequency, has dates equivalent to the source. It is usually not appropriate to use this mode with:

  • Period-end frequencies : ‘A’, ‘Y’, ‘Q’ or ‘M’, in opposition to ‘AS’ ‘YS’, ‘QS’ and ‘MS’

  • Sub-monthly frequencies that do not divide a day evenly‘W’, ‘nD’ where N != 1

    or ‘mH’ where 24 % m != 0).

If one of the source or target calendars is “360_day”, align_on must be specified and two options are offered.

  • “year”

    The dates are translated according to their relative position in the year, ignoring their original month and day information, meaning that the missing/surplus days are added/removed at regular intervals.

    From a 360_day to a standard calendar, the output will be missing the following dates (day of year in parentheses):

    To a leap year:

    January 31st (31), March 31st (91), June 1st (153), July 31st (213), September 31st (275) and November 30th (335).

    To a non-leap year:

    February 6th (36), April 19th (109), July 2nd (183), September 12th (255), November 25th (329).

    From a standard calendar to a “360_day”, the following dates in the source array will be dropped:

    From a leap year:

    January 31st (31), April 1st (92), June 1st (153), August 1st (214), September 31st (275), December 1st (336)

    From a non-leap year:

    February 6th (37), April 20th (110), July 2nd (183), September 13th (256), November 25th (329)

    This option is best used on daily and subdaily data.

  • “date”

    The month/day information is conserved and invalid dates are dropped from the output. This means that when converting from a “360_day” to a standard calendar, all 31st (Jan, March, May, July, August, October and December) will be missing as there is no equivalent dates in the “360_day” calendar and the 29th (on non-leap years) and 30th of February will be dropped as there are no equivalent dates in a standard calendar.

    This option is best used with data on a frequency coarser than daily.