Skip to content

rolling argmin() or argmax() gives the wrong result (are they inverted?) #8541

Closed
@JulianGiles

Description

@JulianGiles

What happened?

I was trying to compute a rolling argmax and argmin on a DataArray, something like:
data.rolling(time=3).argmax()

What did you expect to happen?

The results of the operations look flipped, the result of rolling argmax looks like what a rolling argmin should give, and viceversa.

Minimal Complete Verifiable Example

import xarray as xr
import numpy as np

# Create a sample DataArray
data = xr.DataArray(np.arange(10), dims='time', coords={'time': np.arange(10)})

# Apply rolling argmax
data.rolling(time=3, center=False).argmax()

# The result should be 2 for every index starting from the third time step. However, the result is always 0 (like argmin?)

# If we try applying argmin
data.rolling(time=3, center=False).argmin()

# Then, the result is always 2, which should be the result for argmax. Are these methods switched?

# By doing it using the reduce() method and passing np.argmax, it works as expected
data.rolling(time=3, center=False).reduce(np.argmax)

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.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.0 | packaged by conda-forge | (main, Oct 3 2023, 08:43:22) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 5.14.21-150400.24.81-default
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: ('de_DE', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.2

xarray: 2023.11.0
pandas: 2.1.3
numpy: 1.26.2
scipy: 1.11.4
netCDF4: 1.6.5
pydap: None
h5netcdf: 1.3.0
h5py: 3.10.0
Nio: None
zarr: None
cftime: 1.6.3
nc_time_axis: None
iris: None
bottleneck: 1.3.7
dask: 2023.12.0
distributed: 2023.12.0
matplotlib: 3.8.2
cartopy: 0.22.0
seaborn: None
numbagg: None
fsspec: 2023.12.1
cupy: None
pint: 0.22
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.2.2
pip: 23.3.1
conda: None
pytest: None
mypy: None
IPython: 8.18.1
sphinx: 7.2.6

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions