|
79 | 79 | "\n", |
80 | 80 | "Xarray provides methods for high-level analysis patterns:\n", |
81 | 81 | "\n", |
82 | | - "1. `rolling` :\n", |
83 | | - " [Operate on rolling or sliding (fixed length, overlapping) windows of your data e.g. running mean.](https://docs.xarray.dev/en/stable/user-guide/computation.html#rolling-window-operations)\n", |
84 | | - "1. `coarsen` :\n", |
85 | | - " [Operate on blocks (fixed length) of your data (downsample).](https://docs.xarray.dev/en/stable/user-guide/computation.html#coarsen-large-arrays)\n", |
86 | | - "1. `groupby` :\n", |
87 | | - " [Parse data into groups (using an exact value) and operate on each one (reduce data).](https://docs.xarray.dev/en/stable/groupby.html)\n", |
88 | | - "1. `groupby_bins`: [GroupBy after discretizing a numeric (non-exact, e.g. float) variable.](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.groupby_bins.html)\n", |
89 | | - "1. `resample` :\n", |
90 | | - " [Groupby specialized for time axes. Either downsample or upsample your data.](https://docs.xarray.dev/en/stable/user-guide/time-series.html#resampling-and-grouped-operations)\n", |
91 | | - "1. `weighted`: [Weight your data before reducing.](https://docs.xarray.dev/en/stable/user-guide/computation.html#weighted-array-reductions) \n", |
| 82 | + "1. [`rolling`](<inv:xarray#*rolling-window-operations>) :\n", |
| 83 | + " Operate on rolling or sliding (fixed length, overlapping) windows of your data e.g. running mean.\n", |
| 84 | + "1. [`coarsen`](<inv:xarray#*coarsen-large-arrays>) :\n", |
| 85 | + " Operate on blocks (fixed length) of your data (downsample).\n", |
| 86 | + "1. [`groupby`](<inv:xarray:py:method:#*.DataArray.groupby>) :\n", |
| 87 | + " Parse data into groups (using an exact value) and operate on each one (reduce data).\n", |
| 88 | + "1. [`groupby_bins`](<inv:xarray:py:method:#*.DataArray.groupby_bins>): GroupBy after discretizing a numeric (non-exact, e.g. float) variable.\n", |
| 89 | + "1. [`resample`](<inv:xarray#*resampling-and-grouped-operations>) :\n", |
| 90 | + " [Groupby specialized for time axes. Either downsample or upsample your data.]\n", |
| 91 | + "1. [`weighted`](<inv:xarray#*weighted-array-reductions>): Weight your data before reducing.\n", |
92 | 92 | "\n", |
93 | 93 | "\n", |
94 | 94 | "```{note}\n", |
|
352 | 352 | "\n", |
353 | 353 | "These are sequential windowed operations with a window of a fixed size.\n", |
354 | 354 | "\n", |
355 | | - "1. `rolling` :\n", |
356 | | - " [Operate on rolling or sliding (fixed length, overlapping) windows of your data e.g. running mean.](https://docs.xarray.dev/en/stable/user-guide/computation.html#rolling-window-operations)\n", |
357 | | - "1. `coarsen` :\n", |
358 | | - " [Operate on blocks (fixed length) of your data (downsample).](https://docs.xarray.dev/en/stable/user-guide/computation.html#coarsen-large-arrays)\n", |
| 355 | + "1. [`rolling`](<inv:xarray#*rolling-window-operations>) :\n", |
| 356 | + " Operate on rolling or sliding (fixed length, overlapping) windows of your data e.g. running mean.\n", |
| 357 | + "1. [`coarsen`](<inv:xarray#*coarsen-large-arrays>) :\n", |
| 358 | + " Operate on blocks (fixed length) of your data (downsample).\n", |
359 | 359 | "\n", |
360 | 360 | "\n", |
361 | 361 | "### Label space\n", |
362 | 362 | "\n", |
363 | 363 | "These are windowed operations with irregular windows based on your data. Members of a single group may be non-sequential and scattered through the dataset.\n", |
364 | 364 | "\n", |
365 | | - "1. `groupby` :\n", |
366 | | - " [Parse data into groups (using an exact value) and operate on each one (reduce data).](https://docs.xarray.dev/en/stable/groupby.html)\n", |
367 | | - "1. `groupby_bins`: [GroupBy after discretizing a numeric (non-exact, e.g. float) variable.](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.groupby_bins.html)\n", |
368 | | - "1. `resample` :\n", |
369 | | - " [Groupby specialized for time axes. Either downsample or upsample your data.](https://docs.xarray.dev/en/stable/user-guide/time-series.html#resampling-and-grouped-operations)\n" |
| 365 | + "1. [`groupby`](<inv:xarray:py:method:#*.DataArray.groupby>) :\n", |
| 366 | + " Parse data into groups (using an exact value) and operate on each one (reduce data).\n", |
| 367 | + "1. [`groupby_bins`](<inv:xarray:py:method:#*.DataArray.groupby_bins> ): GroupBy after discretizing a numeric (non-exact, e.g. float) variable.\n", |
| 368 | + "1. [`resample`](<inv:xarray#*resampling-and-grouped-operations>) :\n", |
| 369 | + " Groupby specialized for time axes. Either downsample or upsample your data.\n" |
370 | 370 | ] |
371 | 371 | }, |
372 | 372 | { |
|
385 | 385 | "\n", |
386 | 386 | "## Index space: windows of fixed width\n", |
387 | 387 | "\n", |
388 | | - "### Sliding windows of fixed length: [`rolling`](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.rolling.html)\n", |
| 388 | + "### Sliding windows of fixed length: [`rolling`](<inv:xarray:py:method:#*.DataArray.rolling>)\n", |
389 | 389 | "\n", |
390 | 390 | "- Supports common reductions : `sum`, `mean`, `count`, `std`, `var` etc.\n", |
391 | 391 | "- Returns object of same shape as input\n", |
|
435 | 435 | "source": [ |
436 | 436 | "#### Apply an existing numpy-only function with `reduce`\n", |
437 | 437 | "\n", |
438 | | - "In some cases, we may want to apply a sliding window function using rolling that is not built in to Xarray. In these cases we can still leverage the sliding windows of rolling and apply our own function with [`reduce`](https://docs.xarray.dev/en/stable/generated/xarray.core.rolling.DataArrayRolling.reduce.html).\n", |
| 438 | + "In some cases, we may want to apply a sliding window function using rolling that is not built in to Xarray. In these cases we can still leverage the sliding windows of rolling and apply our own function with [`reduce`](<inv:xarray:py:method:#*.DataArrayRolling.reduce>).\n", |
439 | 439 | "\n", |
440 | 440 | "The `reduce` method on Xarray objects (e.g. DataArray, Dataset) expects a function that can *receive and return plain arrays (e.g. numpy)*, as in each of the \"windows\" provided by the rolling iterator. This is in contrast to the `map` method on DataArray and Dataset objects, which expects a function that can receive and return Xarray objects.\n", |
441 | 441 | "\n", |
442 | | - "Here's an example function: [`np.ptp`](https://numpy.org/doc/stable/reference/generated/numpy.ptp.html).\n" |
| 442 | + "Here's an example function: [`np.ptp`](<inv:numpy:py:function:#*numpy.ptp>).\n" |
443 | 443 | ] |
444 | 444 | }, |
445 | 445 | { |
|
461 | 461 | ":class: tip\n", |
462 | 462 | "\n", |
463 | 463 | "Calculate the rolling mean in 5 point bins along both latitude and longitude using\n", |
464 | | - "[`rolling(...).reduce`](https://docs.xarray.dev/en/stable/generated/xarray.core.rolling.DataArrayRolling.reduce.html)\n", |
| 464 | + "[`rolling(...).reduce`](<inv:xarray:py:method:#*.DataArrayRolling.reduce>)\n", |
465 | 465 | "\n", |
466 | 466 | ":::{admonition} Solution\n", |
467 | 467 | ":class: dropdown\n", |
|
486 | 486 | "source": [ |
487 | 487 | "#### View the `rolling` operation as a Xarray object with `construct`\n", |
488 | 488 | "\n", |
489 | | - "In the above examples, we plotted the outputs of our rolling operations. Xarray makes it easy to integrate the outputs from `rolling` directly into the DataArray using the [`construct`](https://docs.xarray.dev/en/stable/generated/xarray.core.rolling.DataArrayRolling.construct.html#xarray.core.rolling.DataArrayRolling.construct) method." |
| 489 | + "In the above examples, we plotted the outputs of our rolling operations. Xarray makes it easy to integrate the outputs from `rolling` directly into the DataArray using the <inv:xarray:py:method:#*.DataArrayRolling.construct> method.\n" |
490 | 490 | ] |
491 | 491 | }, |
492 | 492 | { |
|
588 | 588 | "\n", |
589 | 589 | "### Block windows of fixed length: `coarsen`\n", |
590 | 590 | "\n", |
591 | | - "For non-overlapping windows or \"blocks\" use [`coarsen`](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.coarsen.html). The syntax is very similar to `rolling`. You will need to specify how you want Xarray to handle the `boundary` if the length of the dimension is not a multiple of the block size.\n", |
| 591 | + "For non-overlapping windows or \"blocks\" use [`coarsen`](<inv:xarray:py:method:#*.DataArray.coarsen>). The syntax is very similar to `rolling`. You will need to specify how you want Xarray to handle the `boundary` if the length of the dimension is not a multiple of the block size.\n", |
592 | 592 | "\n", |
593 | 593 | "\n", |
594 | 594 | "- Supports common reductions : `sum`, `mean`, `count`, `std`, `var` etc.\n", |
|
759 | 759 | "metadata": {}, |
760 | 760 | "source": [ |
761 | 761 | "Note that `coarsen` pads with NaNs. For more control over padding, use\n", |
762 | | - "[DataArray.pad](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.pad.html) explicitly." |
| 762 | + "[DataArray.pad](<inv:xarray:py:method:#*.DataArray.pad>) explicitly." |
763 | 763 | ] |
764 | 764 | }, |
765 | 765 | { |
|
910 | 910 | "1. *Combine* : concatenate results from apply step along a new \"group\" dimension\n", |
911 | 911 | "\n", |
912 | 912 | "illustrated in this neat schematic from [Project Pythia](https://foundations.projectpythia.org/core/xarray/computation-masking.html#groupby-split-apply-combine):\n", |
913 | | - "<img src=\"https://foundations.projectpythia.org/_images/xarray-split-apply-combine.jpeg\" width=30%>\n", |
914 | | - "\n", |
| 913 | + "<img src=\"https://raw.githubusercontent.com/ProjectPythia/pythia-foundations/refs/heads/main/core/xarray/images/xarray-split-apply-combine.jpeg\" width=30%>\n", |
915 | 914 | "\n", |
916 | 915 | "But really there is a \"hidden\" first step: *identifying* groups (also called \"factorization\" or sometimes \"binning\"). Usually this is the hard part.\n", |
917 | 916 | "\n", |
|
1036 | 1035 | "\n", |
1037 | 1036 | "If the automatic group detection doesn't work for your problem then these functions are useful for constructing specific \"group labels\" in many cases\n", |
1038 | 1037 | "\n", |
1039 | | - "1. [numpy.digitize](https://numpy.org/doc/stable/reference/generated/numpy.digitize.html)\n", |
| 1038 | + "1. [numpy.digitize](<inv:numpy:py:function:#*numpy.digitize>)\n", |
1040 | 1039 | " for binning\n", |
1041 | | - "1. [numpy.searchsorted](https://numpy.org/doc/stable/reference/generated/numpy.searchsorted.html)\n", |
| 1040 | + "1. [numpy.searchsorted](<inv:numpy:py:function:#*numpy.searchsorted>)\n", |
1042 | 1041 | " supports many other data types\n", |
1043 | | - "1. [pandas.factorize](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.factorize.html)\n", |
| 1042 | + "1. [pandas.factorize](<inv:pandas:py:function:#*pandas.factorize>)\n", |
1044 | 1043 | " supports characters, strings etc.\n", |
1045 | | - "1. [pandas.cut](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html)\n", |
| 1044 | + "1. [pandas.cut](<inv:pandas:py:function:#*pandas.cut>)\n", |
1046 | 1045 | " for binning\n", |
1047 | | - "1. [\"Datetime components\"](https://docs.xarray.dev/en/stable/user-guide/time-series.html#datetime-components) of Xarray DataArrays\n", |
1048 | | - "1. [DataArray.isin](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.isin.html)\n", |
1049 | | - "1. [scipy.ndimage.label](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html)\n", |
| 1046 | + "1. [\"Datetime components\"](<inv:xarray#*indexing-using-datetime-components>) of Xarray DataArrays\n", |
| 1047 | + "1. [DataArray.isin](<inv:xarray:py:method:#*.DataArray.isin>)\n", |
| 1048 | + "1. [scipy.ndimage.label](<inv:scipy:py:function:#*scipy.ndimage.label>)\n", |
1050 | 1049 | "1. Do you know of any more? (Send in a pull request to update this list!)\n", |
1051 | 1050 | "\n", |
1052 | 1051 | "\n", |
1053 | | - "\n", |
1054 | 1052 | "```{tip}\n", |
1055 | | - "Xarray uses [`pandas.factorize`](https://pandas.pydata.org/docs/reference/api/pandas.factorize.html) for `groupby` and [`pandas.cut`](https://pandas.pydata.org/docs/reference/api/pandas.cut.html) for `groupby_bins`.\n", |
| 1053 | + "Xarray uses `pandas.factorize` for `groupby` and `pandas.cut` for `groupby_bins`.\n", |
1056 | 1054 | "```" |
1057 | 1055 | ] |
1058 | 1056 | }, |
|
1066 | 1064 | "tags": [] |
1067 | 1065 | }, |
1068 | 1066 | "source": [ |
1069 | | - "#### [\"Datetime components\"](https://docs.xarray.dev/en/stable/user-guide/time-series.html#datetime-components) for creating groups\n", |
| 1067 | + "#### \"Datetime components\" for creating groups\n", |
1070 | 1068 | "\n", |
1071 | | - "See a full list\n", |
1072 | | - "[here](https://docs.xarray.dev/en/stable/generated/xarray.core.accessor_dt.DatetimeAccessor.html?highlight=DatetimeAccessor)\n", |
| 1069 | + "See a full list [here](<inv:xarray:py:class#*DatetimeAccessor>)\n", |
1073 | 1070 | "\n", |
1074 | 1071 | "These can be accessed in a few different ways as illustrated below.\n" |
1075 | 1072 | ] |
|
1229 | 1226 | "source": [ |
1230 | 1227 | "##### `floor`, `ceil` and `round` on time\n", |
1231 | 1228 | "\n", |
1232 | | - "Additional functionality in the [datetime accessor](https://docs.xarray.dev/en/stable/generated/xarray.core.accessor_dt.DatetimeAccessor.html) allows us to effectively \"resample\" our time data to remove roundoff errors in timestamps.\n" |
| 1229 | + "Additional functionality in the [datetime accessor](<inv:xarray:py:class#*DatetimeAccessor>) allows us to effectively \"resample\" our time data to remove roundoff errors in timestamps.\n" |
1233 | 1230 | ] |
1234 | 1231 | }, |
1235 | 1232 | { |
|
1449 | 1446 | "\n", |
1450 | 1447 | "Xarray provides methods for high-level analysis patterns:\n", |
1451 | 1448 | "\n", |
1452 | | - "1. `rolling` :\n", |
1453 | | - " [Operate on rolling (fixed length, overlapping) windows of your data e.g. running mean.](https://docs.xarray.dev/en/stable/user-guide/computation.html#rolling-window-operations)\n", |
1454 | | - "1. `coarsen` :\n", |
1455 | | - " [Operate on blocks (fixed length) of your data (downsample).](https://docs.xarray.dev/en/stable/user-guide/computation.html#coarsen-large-arrays)\n", |
1456 | | - "1. `groupby` :\n", |
1457 | | - " [Parse data into groups (using an exact value) and operate on each one (reduce data).](https://docs.xarray.dev/en/stable/groupby.html)\n", |
1458 | | - "1. `groupby_bins`: [GroupBy after discretizing a numeric (non-exact, e.g. float) variable.](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.groupby_bins.html)\n", |
1459 | | - "1. `resample` :\n", |
1460 | | - " [Groupby specialized for time axes. Either downsample or upsample your data.](https://docs.xarray.dev/en/stable/user-guide/time-series.html#resampling-and-grouped-operations)\n", |
1461 | | - "1. `weighted`: [Weight your data before reducing.](https://docs.xarray.dev/en/stable/user-guide/computation.html#weighted-array-reductions)\n", |
| 1449 | + "1. [`rolling`](<inv:xarray#*rolling-window-operations>) :\n", |
| 1450 | + " Operate on rolling or sliding (fixed length, overlapping) windows of your data e.g. running mean.\n", |
| 1451 | + "1. [`coarsen`](<inv:xarray#*coarsen-large-arrays>) :\n", |
| 1452 | + " Operate on blocks (fixed length) of your data (downsample).\n", |
| 1453 | + "1. [`groupby`](<inv:xarray:py:method:#*.DataArray.groupby>) :\n", |
| 1454 | + " Parse data into groups (using an exact value) and operate on each one (reduce data).\n", |
| 1455 | + "1. [`groupby_bins`](<inv:xarray:py:method:#*.DataArray.groupby_bins>): GroupBy after discretizing a numeric (non-exact, e.g. float) variable.\n", |
| 1456 | + "1. [`resample`](<inv:xarray#*resampling-and-grouped-operations>) :\n", |
| 1457 | + " [Groupby specialized for time axes. Either downsample or upsample your data.]\n", |
| 1458 | + "1. [`weighted`](<inv:xarray#*weighted-array-reductions>): Weight your data before reducing.\n", |
1462 | 1459 | "\n", |
1463 | 1460 | "Xarray also provides a consistent interface to make using those patterns easy:\n", |
1464 | 1461 | "\n", |
|
0 commit comments