Adaptor function that translates our groupby API to that of flox.
(
self,
dim: Dims,
keep_attrs: bool | None = None,
**kwargs: Any,
)
| 1062 | return parsed_dim |
| 1063 | |
| 1064 | def _flox_reduce( |
| 1065 | self, |
| 1066 | dim: Dims, |
| 1067 | keep_attrs: bool | None = None, |
| 1068 | **kwargs: Any, |
| 1069 | ) -> T_Xarray: |
| 1070 | """Adaptor function that translates our groupby API to that of flox.""" |
| 1071 | import flox |
| 1072 | from flox.xarray import xarray_reduce |
| 1073 | |
| 1074 | from xarray.core.dataset import Dataset |
| 1075 | |
| 1076 | obj = self._original_obj |
| 1077 | variables = ( |
| 1078 | {k: v.variable for k, v in obj.data_vars.items()} |
| 1079 | if isinstance(obj, Dataset) # type: ignore[redundant-expr] # seems to be a mypy bug |
| 1080 | else obj._coords |
| 1081 | ) |
| 1082 | |
| 1083 | if keep_attrs is None: |
| 1084 | keep_attrs = _get_keep_attrs(default=True) |
| 1085 | |
| 1086 | if Version(flox.__version__) < Version("0.9") and not self._by_chunked: |
| 1087 | # preserve current strategy (approximately) for dask groupby |
| 1088 | # on older flox versions to prevent surprises. |
| 1089 | # flox >=0.9 will choose this on its own. |
| 1090 | kwargs.setdefault("method", "cohorts") |
| 1091 | |
| 1092 | midx_grouping_vars: tuple[Hashable, ...] = () |
| 1093 | for grouper in self.groupers: |
| 1094 | name = grouper.name |
| 1095 | maybe_midx = obj._indexes.get(name, None) |
| 1096 | if isinstance(maybe_midx, PandasMultiIndex): |
| 1097 | midx_grouping_vars += tuple(maybe_midx.index.names) + (name,) |
| 1098 | |
| 1099 | # For datasets, running a numeric-only reduction on non-numeric |
| 1100 | # variable will just drop it. |
| 1101 | non_numeric: dict[Hashable, Variable] |
| 1102 | if kwargs.pop("numeric_only", None): |
| 1103 | non_numeric = { |
| 1104 | name: var |
| 1105 | for name, var in variables.items() |
| 1106 | if ( |
| 1107 | not _is_numeric_aggregatable_dtype(var) |
| 1108 | # this avoids dropping any levels of a MultiIndex, which raises |
| 1109 | # a warning |
| 1110 | and name not in midx_grouping_vars |
| 1111 | and name not in obj.dims |
| 1112 | ) |
| 1113 | } |
| 1114 | else: |
| 1115 | non_numeric = {} |
| 1116 | |
| 1117 | if "min_count" in kwargs: |
| 1118 | if kwargs["func"] not in ["sum", "prod"]: |
| 1119 | raise TypeError("Received an unexpected keyword argument 'min_count'") |
| 1120 | elif kwargs["min_count"] is None: |
| 1121 | # set explicitly to avoid unnecessarily accumulating count |
no test coverage detected