|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "<img src=\"https://docs.xarray.dev/en/stable/_static/dataset-diagram-logo.png\" align=\"right\" width=\"30%\">\n", |
8 | | - "\n", |
9 | 7 | "# Grouped Computations\n", |
10 | 8 | "\n", |
11 | 9 | "In this lesson, we discuss how to do scientific computations with defined \"groups\" of data\n", |
|
29 | 27 | "import matplotlib.pyplot as plt\n", |
30 | 28 | "\n", |
31 | 29 | "# don't expand data by default\n", |
32 | | - "xr.set_options(display_expand_data=False)\n", |
| 30 | + "xr.set_options(display_expand_data=False, display_expand_attrs=False)\n", |
33 | 31 | "\n", |
34 | 32 | "%config InlineBackend.figure_format='retina'" |
35 | 33 | ] |
|
217 | 215 | "`gb` is a DatasetGroupBy object. It represents a GroupBy operation and helpfully tells us the unique \"groups\" or labels found during the split step.\n", |
218 | 216 | "\n", |
219 | 217 | "\n", |
220 | | - "<div class=\"alert alert-info\">\n", |
221 | | - " Xarrays' computation methods (groupby, groupby_bins, rolling, coarsen, weighted) all return special objects that represent the basic underlying computation pattern. For e.g. `gb` above is a `DatasetGroupBy` object that represents monthly groupings of the data in `ds` . It is usually helpful to save and reuse these objects for multiple operations (e.g. a mean and standard deviation calculation).\n", |
222 | | - "</div>" |
| 218 | + "```{tip}\n", |
| 219 | + "\n", |
| 220 | + "Xarrays' computation methods (`groupby`, `groupby_bins`, `rolling`, `coarsen`, `weighted`) all return special objects that represent the basic underlying computation pattern. For e.g. `gb` above is a `DatasetGroupBy` object that represents monthly groupings of the data in `ds` . It is usually helpful to save and reuse these objects for multiple operations (e.g. a mean and standard deviation calculation).\n", |
| 221 | + "```" |
223 | 222 | ] |
224 | 223 | }, |
225 | 224 | { |
|
480 | 479 | "cell_type": "markdown", |
481 | 480 | "metadata": {}, |
482 | 481 | "source": [ |
483 | | - "### Exercise\n", |
| 482 | + "```{exercise} \n", |
| 483 | + ":label: annual-mean\n", |
484 | 484 | "\n", |
485 | | - "Using `groupby`, plot the annual mean time series of SST at 300°E, 50°N" |
486 | | - ] |
487 | | - }, |
488 | | - { |
489 | | - "cell_type": "code", |
490 | | - "execution_count": null, |
491 | | - "metadata": { |
492 | | - "tags": [ |
493 | | - "hide-output" |
494 | | - ] |
495 | | - }, |
496 | | - "outputs": [], |
497 | | - "source": [ |
498 | | - "ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50).plot();" |
| 485 | + "Using `groupby`, plot the annual mean time series of SST at 300°E, 50°N\n", |
| 486 | + "```\n", |
| 487 | + "````{solution} annual-mean\n", |
| 488 | + ":class: dropdown\n", |
| 489 | + "```python\n", |
| 490 | + "ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50).plot();\n", |
| 491 | + "```\n", |
| 492 | + "````" |
499 | 493 | ] |
500 | 494 | }, |
501 | 495 | { |
|
539 | 533 | "cell_type": "markdown", |
540 | 534 | "metadata": {}, |
541 | 535 | "source": [ |
542 | | - "<div class=\"alert alert-info\">\n", |
543 | | - " <strong>Note:</strong> <code>resample</code> only works with proper datetime64 coordinate labels. Note the `dtype` of `time` in the repr above.\n", |
544 | | - "</div>" |
| 536 | + "```{note}\n", |
| 537 | + "`resample` only works with proper datetime64 coordinate labels. Note the `dtype` of `time` in the repr above.\n", |
| 538 | + "```" |
545 | 539 | ] |
546 | 540 | }, |
547 | 541 | { |
|
588 | 582 | "tags": [] |
589 | 583 | }, |
590 | 584 | "source": [ |
591 | | - "### Exercise\n", |
| 585 | + "```{exercise}\n", |
| 586 | + ":label: resample-mean\n", |
592 | 587 | "\n", |
593 | 588 | "Using `resample`, plot the annual mean time series of SST at 300°E, 50°N.\n", |
594 | 589 | "\n", |
595 | | - "Compare this output to the groupby output. What differences do you see?" |
596 | | - ] |
597 | | - }, |
598 | | - { |
599 | | - "cell_type": "code", |
600 | | - "execution_count": null, |
601 | | - "metadata": { |
602 | | - "tags": [ |
603 | | - "hide-output", |
604 | | - "hide-input" |
605 | | - ] |
606 | | - }, |
607 | | - "outputs": [], |
608 | | - "source": [ |
| 590 | + "Compare this output to the groupby output. What differences do you see?\n", |
| 591 | + "```\n", |
| 592 | + "````{solution} resample-mean\n", |
| 593 | + ":class: dropdown\n", |
| 594 | + "```python\n", |
609 | 595 | "resampled = ds.resample(time='Y').mean().sst.sel(lon=300, lat=50)\n", |
610 | | - "resampled.plot();" |
| 596 | + "resampled.plot();\n", |
| 597 | + "```\n", |
| 598 | + "````" |
611 | 599 | ] |
612 | 600 | }, |
613 | 601 | { |
|
618 | 606 | ] |
619 | 607 | }, |
620 | 608 | "source": [ |
| 609 | + "## GroupBy vs Resample \n", |
| 610 | + "\n", |
621 | 611 | "Let's compare the grouped and resampled outputs.\n", |
622 | 612 | "\n", |
623 | 613 | "\n", |
|
630 | 620 | "cell_type": "code", |
631 | 621 | "execution_count": null, |
632 | 622 | "metadata": { |
633 | | - "tags": [ |
634 | | - "hide-output", |
635 | | - "hide-input" |
636 | | - ] |
| 623 | + "tags": [] |
637 | 624 | }, |
638 | 625 | "outputs": [], |
639 | 626 | "source": [ |
640 | 627 | "from IPython.display import display_html\n", |
641 | 628 | "\n", |
642 | 629 | "grouped = ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50)\n", |
| 630 | + "resampled = ds.resample(time='Y').mean().sst.sel(lon=300, lat=50)\n", |
643 | 631 | "display_html(grouped)\n", |
644 | 632 | "display_html(resampled)" |
645 | 633 | ] |
|
648 | 636 | "cell_type": "code", |
649 | 637 | "execution_count": null, |
650 | 638 | "metadata": { |
651 | | - "tags": [ |
652 | | - "hide-input", |
653 | | - "hide-output" |
654 | | - ] |
| 639 | + "tags": [] |
655 | 640 | }, |
656 | 641 | "outputs": [], |
657 | 642 | "source": [ |
|
0 commit comments