{ "cells": [ { "cell_type": "markdown", "id": "ad17d3f7-f702-4444-acd2-a1a79fea00f3", "metadata": {}, "source": [ "# Getting Seasonal Means for the season December-January-February\n", "\n", "We've been getting occasionally a few questions about seasonal reduction operations (for example means).\n", "\n", "Say you have a dataset that goes on for several years, and you want to compare the seaonal means of each year against each other.\n", "\n", "For three of the four seasons this isn't a big deal, you cut the dataset into yearly chunks, then mean over the season.\n", "\n", "But for the fourth season (DJF) this doesn't work: It bunches together the January, February, and December of that same year, when in fact you want the December from the year before.\n", "\n", "Let's have a look at an overly simplified example.\n", "\n", "## Our example dataset\n", "\n", "We only use a single dimension of data, and we only use monthly values. \n", "What's more, the values of our \"dataset\" are strictly ascending. \n", "\n", "This keeps the dataset small and we can easily see what's going on.\n", "\n", "The code works just the same way with far more complex datasets" ] }, { "cell_type": "code", "execution_count": 1, "id": "75b07cb3-ec82-41b4-b480-070c04599c04", "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "id": "fcc0e15e-c4ee-49b1-9137-c0bcdd8c2b4a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (time: 60)>\n", "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59])\n", "Coordinates:\n", " * time (time) datetime64[ns] 2000-01-01 2000-02-01 ... 2004-12-01
<xarray.DataArray (season: 4)>\n", "array([28., 30., 27., 33.])\n", "Coordinates:\n", " * season (season) object 'DJF' 'JJA' 'MAM' 'SON'
<xarray.DataArray (time: 20)>\n", "array([ 1., 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37.,\n", " 40., 43., 46., 49., 52., 55., 58.])\n", "Coordinates:\n", " * time (time) datetime64[ns] 2000-01-01 2000-04-01 ... 2004-10-01
<xarray.DataArray (time: 21)>\n", "array([ 0.5, 3. , 6. , 9. , 12. , 15. , 18. , 21. , 24. , 27. , 30. ,\n", " 33. , 36. , 39. , 42. , 45. , 48. , 51. , 54. , 57. , 59. ])\n", "Coordinates:\n", " * time (time) datetime64[ns] 1999-12-01 2000-03-01 ... 2004-12-01
<xarray.DataArray (time: 6)>\n", "array([ 0.5, 12. , 24. , 36. , 48. , 59. ])\n", "Coordinates:\n", " * time (time) datetime64[ns] 1999-12-01 2000-12-01 ... 2004-12-01
<xarray.DataArray (time: 6)>\n", "array([ 0.5, 12. , 24. , 36. , 48. , 59. ])\n", "Coordinates:\n", " * time (time) datetime64[ns] 2000-02-29 2001-02-28 ... 2005-02-28
<xarray.DataArray (year: 6)>\n", "array([ 0.5, 12. , 24. , 36. , 48. , 59. ])\n", "Coordinates:\n", " time (year) datetime64[ns] 2000-02-29 2001-02-28 ... 2005-02-28\n", " * year (year) int64 2000 2001 2002 2003 2004 2005