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

More groupby indexing problems #7797

Closed
4 tasks
slevang opened this issue Apr 29, 2023 · 1 comment · Fixed by #7798
Closed
4 tasks

More groupby indexing problems #7797

slevang opened this issue Apr 29, 2023 · 1 comment · Fixed by #7798

Comments

@slevang
Copy link
Contributor

slevang commented Apr 29, 2023

What happened?

There is still something wrong with the groupby indexing changes from 2023.04.0 onward. Here are two typical ways of doing a seasonal anomaly calculation, which return the same result on 2023.03.0 but are different on 2023.04.2:

import numpy as np
import xarray as xr

# 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
anom_sel = x_slice - clim.sel(month=x_slice.time.dt.month)

# passes on 2023.3.0, fails on 2023.4.2
# the groupby version is aligning the indexes wrong, giving us something other than 0
assert anom_sel.equals(anom_gb)

Related: #7759 #7766
cc @dcherian

What did you expect to happen?

No response

Minimal Complete Verifiable Example

No response

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

No response

Anything else we need to know?

No response

Environment

@slevang slevang added bug needs triage Issue that has not been reviewed by xarray team member labels Apr 29, 2023
dcherian added a commit to dcherian/xarray that referenced this issue Apr 30, 2023
@dcherian
Copy link
Contributor

dcherian commented Apr 30, 2023

x_slice.groupby("time.month") - clim

Nice, we test x.groupby("time.month") - clim.slice(...) but not x.sel(...).groupby() - clim

Is it possible for you to run nightly tests against xarray's main branch? That would help a lot.

@dcherian dcherian added topic-groupby and removed needs triage Issue that has not been reviewed by xarray team member labels Apr 30, 2023
dcherian added a commit that referenced this issue May 2, 2023
…#7798)

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

Closes #7797

* Fix tests

Co-authored-by: Alan Brammer <a1brammer@gmail.com>
Co-authored-by: Mick <mick.niklas@gmail.com>

* fix doc build

* [skip-ci] Update doc/whats-new.rst

---------

Co-authored-by: Alan Brammer <a1brammer@gmail.com>
Co-authored-by: Mick <mick.niklas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants