Modifying min(dataarray) modifies dataarray
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:
Getting the minimum of a data array returns a reference to that minimum data point such that when modifying the minimum, the original data array is changed as well (same for max).
What you expected to happen:
Not sure if this is intended or a bug, but I expected an independent data array (the same as using dataarray.min()). This behaviour also differs from numpy (see below).
Minimal Complete Verifiable Example:
import xarray as xr
import numpy as np
x_arr = xr.DataArray([1,2,3])
min_x = min(x_arr)
min_x += 10
x_arr
# [11,2,3]
n_arr = np.array([1,2,3])
min_n = min(n_arr)
min_n += 10
n_arr
# [1,2,3]
Environment:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.9 (default, Aug 18 2020, 06:24:24)
[GCC 5.4.0 20160609]
python-bits: 64
OS: Linux
OS-release: 4.4.0-189-lowlatency
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.6.3
xarray: 0.16.1
pandas: 1.0.3
numpy: 1.18.5
scipy: 1.5.4
netCDF4: 1.5.4
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.1.3
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.2.1
cartopy: None
seaborn: None
numbagg: None
pint: 0.16.1
setuptools: 50.3.0
pip: 19.0.3
conda: None
pytest: None
IPython: 7.13.0
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal Python example comparing built-in min(dataarray), DataArray.min(), and NumPy min behavior. Trace the entry point used by min(dataarray), then add a regression test showing that modifying the returned minimum or maximum does not modify the original DataArray; done means the original values remain unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100