MCPcopy Index your code
hub / github.com/pydata/xarray / GroupByAggregationGenerator

Class GroupByAggregationGenerator

xarray/util/generate_aggregations.py:490–556  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

488
489
490class GroupByAggregationGenerator(AggregationGenerator):
491 _dim_docstring = _DIM_DOCSTRING_GROUPBY
492 _template_signature = TEMPLATE_REDUCTION_SIGNATURE_GROUPBY
493
494 def generate_code(self, method, has_keep_attrs):
495 extra_kwargs = [kwarg.call for kwarg in method.extra_kwargs if kwarg.call]
496
497 if self.datastructure.numeric_only:
498 extra_kwargs.append(f"numeric_only={method.numeric_only},")
499
500 # median isn't enabled yet, because it would break if a single group was present in multiple
501 # chunks. The non-flox code path will just rechunk every group to a single chunk and execute the median
502 method_is_not_flox_supported = method.name in ("median", "cumprod")
503 if method_is_not_flox_supported:
504 indent = 12
505 else:
506 indent = 16
507
508 if extra_kwargs:
509 extra_kwargs = textwrap.indent("\n" + "\n".join(extra_kwargs), indent * " ")
510 else:
511 extra_kwargs = ""
512
513 if method.aggregation_type == "scan":
514 # Scans retain dimensions.
515 out_finalized = "out.assign_coords(self._obj.coords)"
516 else:
517 out_finalized = "out"
518
519 if method_is_not_flox_supported:
520 return f"""\
521 out = self.reduce(
522 duck_array_ops.{method.array_method},
523 dim=dim,{extra_kwargs}
524 keep_attrs=keep_attrs,
525 **kwargs,
526 )
527 return {out_finalized}"""
528
529 min_version_check = f"""
530 and module_available("flox", minversion="{method.min_flox_version}")"""
531
532 return (
533 """\
534 if (
535 flox_available
536 and OPTIONS["use_flox"]"""
537 + (min_version_check if method.min_flox_version is not None else "")
538 + f"""
539 and contains_only_chunked_or_numpy(self._obj)
540 ):
541 return self._flox_{method.aggregation_type}(
542 func="{method.name}",
543 dim=dim,{extra_kwargs}
544 # fill_value=fill_value,
545 keep_attrs=keep_attrs,
546 **kwargs,
547 )

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…