Apply a function over each Dataset in the groups generated for resampling and concatenate them together into a new Dataset. `func` is called like `func(ds, *args, **kwargs)` for each dataset `ds` in this group. Apply uses heuristics (like `pandas.GroupBy.apply`) to
(
self,
func: Callable[..., Any],
args: tuple[Any, ...] = (),
shortcut: bool | None = None,
**kwargs: Any,
)
| 398 | """DatasetGroupBy object specialized to resampling a specified dimension""" |
| 399 | |
| 400 | def map( |
| 401 | self, |
| 402 | func: Callable[..., Any], |
| 403 | args: tuple[Any, ...] = (), |
| 404 | shortcut: bool | None = None, |
| 405 | **kwargs: Any, |
| 406 | ) -> Dataset: |
| 407 | """Apply a function over each Dataset in the groups generated for |
| 408 | resampling and concatenate them together into a new Dataset. |
| 409 | |
| 410 | `func` is called like `func(ds, *args, **kwargs)` for each dataset `ds` |
| 411 | in this group. |
| 412 | |
| 413 | Apply uses heuristics (like `pandas.GroupBy.apply`) to figure out how |
| 414 | to stack together the datasets. The rule is: |
| 415 | |
| 416 | 1. If the dimension along which the group coordinate is defined is |
| 417 | still in the first grouped item after applying `func`, then stack |
| 418 | over this dimension. |
| 419 | 2. Otherwise, stack over the new dimension given by name of this |
| 420 | grouping (the argument to the `groupby` function). |
| 421 | |
| 422 | Parameters |
| 423 | ---------- |
| 424 | func : callable |
| 425 | Callable to apply to each sub-dataset. |
| 426 | args : tuple, optional |
| 427 | Positional arguments passed on to `func`. |
| 428 | **kwargs |
| 429 | Used to call `func(ds, **kwargs)` for each sub-dataset `ar`. |
| 430 | |
| 431 | Returns |
| 432 | ------- |
| 433 | applied : Dataset |
| 434 | The result of splitting, applying and combining this dataset. |
| 435 | """ |
| 436 | # ignore shortcut if set (for now) |
| 437 | applied = (func(ds, *args, **kwargs) for ds in self._iter_grouped()) |
| 438 | combined = self._combine(applied) |
| 439 | |
| 440 | # If the aggregation function didn't drop the original resampling |
| 441 | # dimension, then we need to do so before we can rename the proxy |
| 442 | # dimension we used. |
| 443 | if self._dim in combined.coords: |
| 444 | combined = combined.drop_vars(self._dim) |
| 445 | |
| 446 | if RESAMPLE_DIM in combined.dims: |
| 447 | combined = combined.rename({RESAMPLE_DIM: self._dim}) |
| 448 | |
| 449 | return combined |
| 450 | |
| 451 | def apply(self, func, args=(), shortcut=None, **kwargs): |
| 452 | """ |