Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix groupby binary ops when grouped array is subset relative to other #7798

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Deprecations

Bug fixes
~~~~~~~~~

Fix groupby binary ops when grouped array is subset relative to other. (:issue:`7797`).
By `Deepak Cherian <https://github.com/dcherian`_.

Documentation
~~~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,9 @@ def _binary_op(self, other, f, reflexive=False):
obj = obj.where(~mask, drop=True)
codes = codes.where(~mask, drop=True).astype(int)

other, _ = align(other, coord, join="outer")
# codes are defined for coord, so we align `other` with `coord`
# before indexing
other, _ = align(other, coord, join="right")
expanded = other.isel({name: codes})

result = g(obj, expanded)
Expand Down
24 changes: 24 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,15 @@ def test_groupby_math_not_aligned(self):
expected = DataArray([10, 11, np.nan, np.nan], array.coords)
assert_identical(expected, actual)

# regression test for #7797
other = array.groupby("b").sum()
actual = array.sel(x=[0, 1]).groupby("b") - other
expected = DataArray([-1, 0], {"b": ("x", [0, 0]), "x": [0, 1]}, dims="x")
assert_identical(expected, actual)

other = DataArray([10], coords={"c": 123, "b": [0]}, dims="b")
actual = array.groupby("b") + other
expected = DataArray([10, 11, np.nan, np.nan], array.coords)
expected.coords["c"] = (["x"], [123] * 2 + [np.nan] * 2)
assert_identical(expected, actual)

Expand Down Expand Up @@ -2289,3 +2296,20 @@ def test_resample_cumsum(method: str, expected_array: list[float]) -> None:
actual = getattr(ds.foo.resample(time="3M"), method)(dim="time")
expected.coords["time"] = ds.time
assert_identical(expected.drop_vars(["time"]).foo, actual)


def test_groupby_binary_op_regression():
dcherian marked this conversation as resolved.
Show resolved Hide resolved
# regression test for #7797
# monthly timeseries that should return "zero anomalies" everywhere
time = xr.date_range("2023-01-01", "2023-12-31", freq="MS")
data = np.linspace(-1, 1, 12)
x = xr.DataArray(data, coords={"time": time})
clim = xr.DataArray(data, coords={"month": np.arange(1, 13, 1)})

# seems to give the correct result if we use the full x, but not with a slice
x_slice = x.sel(time=["2023-04-01"])

# two typical ways of computing anomalies
anom_gb = x_slice.groupby("time.month") - clim

assert_identical(xr.zeros_like(anom_gb), anom_gb)