xarray.Dataset.temporal.average#

Dataset.temporal.average(data_var, weighted=True, keep_weights=False)#

Returns a Dataset with the average of a data variable and the time dimension removed.

This method infers the time grouping frequency by checking the distance between a set of upper and lower time bounds. This method is particularly useful for calculating the weighted averages of monthly or yearly time series data because the number of days per month/year can vary based on the calendar type, which can affect weighting. For other frequencies, the distribution of weights will be equal so weighted=True is the same as weighted=False.

Time bounds are used for inferring the time series frequency and for generating weights (refer to the weighted parameter documentation below).

Parameters:
  • data_var (str) – The key of the data variable for calculating averages

  • weighted (bool, optional) – Calculate averages using weights, by default True.

    Weights are calculated by first determining the length of time for each coordinate point using the difference of its upper and lower bounds. The time lengths are grouped, then each time length is divided by the total sum of the time lengths to get the weight of each coordinate point.

    The weight of masked (missing) data is excluded when averages are taken. This is the same as giving them a weight of 0.

  • keep_weights (bool, optional) – If calculating averages using weights, keep the weights in the final dataset output, by default False.

Returns:

xr.Dataset – Dataset with the average of the data variable and the time dimension removed.

Examples

Get weighted averages for a monthly time series data variable:

>>> ds_month = ds.temporal.average("ts")
>>> ds_month.ts