pydata / pydata/xarray

Anomaly calculation with groupby leaves seasonal cycle

Open
#7,838 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

When calculating EOFS with sea surface temperature (SST) anomaly, We see the seasonal cycle still in the data.
Tested with xarray v2023.03.0 and v202304.02, both have the same problem (seasonal cycle still in the data).
So the anomaly calculated with groupby is not correct.

Attached is the results of using xarray v2023.04.02
Screenshot 2023-05-11 at 11 31 30 PM

What did you expect to happen?

The EOFs will not have seasonal cycle. Attached is the results from xarray v0.20.2
Screenshot 2023-05-11 at 1 37 32 PM

Minimal Complete Verifiable Example
import xarray as xr
import numpy as np
import pandas as pd
from eofs.xarray import Eof

url = 'http://kage.ldeo.columbia.edu:81/SOURCES/.LOCAL/.sst.mon.mean.nc/.sst/dods'
ds = xr.open_dataset(url)

gb = ds.sst.groupby('time.month')
ds_anom = (gb - gb.mean(dim='time')).compute()
solver = Eof(ds_anom)
pcs = solver.pcs(npcs=4,pcscaling=1)
eofs = solver.eofsAsCorrelation(neofs=4)

eofs.plot(x='lon',y='lat',col='mode',col_wrap=4,add_colorbar=1)
pcs.plot(x='time',col='mode',col_wrap=4);
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
INSTALLED VERSIONS ------------------ commit: None python: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0] python-bits: 64 OS: Linux OS-release: 4.13.16-100.fc25.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.8.1

xarray: 2023.4.2
pandas: 1.5.3
numpy: 1.23.5
scipy: 1.10.1
netCDF4: 1.6.0
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: 2023.4.0
distributed: 2023.4.0
matplotlib: 3.7.1
cartopy: 0.21.1
seaborn: None
numbagg: None
fsspec: 2023.4.0
cupy: None
pint: None
sparse: 0.14.0
flox: None
numpy_groupies: None
setuptools: 66.0.0
pip: 23.0.1
conda: None
pytest: None
mypy: None
IPython: 8.12.0
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file or test. Start by running the provided Python MVCE and inspecting the groupby anomaly calculation and its result across the reported xarray versions. Done means the anomaly no longer retains the seasonal cycle and the behavior is covered by a reproducible test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.