Skip to content

Commit

Permalink
Bugfix DataArray resampling.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Nov 22, 2021
1 parent 8f23310 commit 4f378a3
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 72 deletions.
293 changes: 222 additions & 71 deletions xarray/core/_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5520,12 +5520,26 @@ def count(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.count,
dim=dim,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="count",
dim=dim,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.count,
dim=dim,
keep_attrs=keep_attrs,
**kwargs,
)

def all(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -5587,12 +5601,26 @@ def all(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.array_all,
dim=dim,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="all",
dim=dim,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.array_all,
dim=dim,
keep_attrs=keep_attrs,
**kwargs,
)

def any(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -5654,12 +5682,26 @@ def any(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.array_any,
dim=dim,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="any",
dim=dim,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.array_any,
dim=dim,
keep_attrs=keep_attrs,
**kwargs,
)

def max(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -5735,13 +5777,28 @@ def max(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.max,
dim=dim,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="max",
dim=dim,
skipna=skipna,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.max,
dim=dim,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)

def min(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -5817,13 +5874,28 @@ def min(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.min,
dim=dim,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="min",
dim=dim,
skipna=skipna,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.min,
dim=dim,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)

def mean(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -5903,13 +5975,28 @@ def mean(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.mean,
dim=dim,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="mean",
dim=dim,
skipna=skipna,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.mean,
dim=dim,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)

def prod(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -6004,14 +6091,30 @@ def prod(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.prod,
dim=dim,
skipna=skipna,
min_count=min_count,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="prod",
dim=dim,
skipna=skipna,
min_count=min_count,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.prod,
dim=dim,
skipna=skipna,
min_count=min_count,
keep_attrs=keep_attrs,
**kwargs,
)

def sum(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -6106,14 +6209,30 @@ def sum(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.sum,
dim=dim,
skipna=skipna,
min_count=min_count,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="sum",
dim=dim,
skipna=skipna,
min_count=min_count,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.sum,
dim=dim,
skipna=skipna,
min_count=min_count,
keep_attrs=keep_attrs,
**kwargs,
)

def std(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -6205,14 +6324,30 @@ def std(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.std,
dim=dim,
skipna=skipna,
ddof=ddof,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="std",
dim=dim,
skipna=skipna,
ddof=ddof,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.std,
dim=dim,
skipna=skipna,
ddof=ddof,
keep_attrs=keep_attrs,
**kwargs,
)

def var(
self: DataArrayGroupByReduce,
Expand Down Expand Up @@ -6304,14 +6439,30 @@ def var(
Coordinates:
* time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31
"""
return self.reduce(
duck_array_ops.var,
dim=dim,
skipna=skipna,
ddof=ddof,
keep_attrs=keep_attrs,
**kwargs,
)

if (
flox
and OPTIONS["use_numpy_groupies"]
and contains_only_dask_or_numpy(self._obj)
):
return self._flox_reduce(
func="var",
dim=dim,
skipna=skipna,
ddof=ddof,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.var,
dim=dim,
skipna=skipna,
ddof=ddof,
keep_attrs=keep_attrs,
**kwargs,
)

def median(
self: DataArrayGroupByReduce,
Expand Down
2 changes: 1 addition & 1 deletion xarray/util/generate_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class DataStructure:
example_call_preamble='.groupby("labels")',
self_type="DataArrayGroupByReduce",
)
DataArrayResampleGenerator = GenericReductionGenerator(
DataArrayResampleGenerator = GroupByReductionGenerator(
cls="Resample",
datastructure=DataArrayObject,
methods=REDUCTION_METHODS,
Expand Down

0 comments on commit 4f378a3

Please sign in to comment.