Assigning nan to int-dtype array converts nan to int
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
(I am almost sure this already exists as an issue, but I can't find the original)
What happened:
When assigning nan to a integer-dtype array, the nan gets incorrectly inverted to int.
What you expected to happen:
I expect to get a ValueError, like I get for pure numpy arrays.
Minimal Complete Verifiable Example:
import xarray
import numpy
da = xarray.DataArray(numpy.array([0,1], dtype="u8"))
da[0] = numpy.nan
print(da)
Gives:
<xarray.DataArray (dim_0: 2)>
array([9223372036854775808, 1], dtype=uint64)
Dimensions without coordinates: dim_0
Anything else we need to know?:
In numpy the equivalent code raises ValueError
This is related but different from #2945. In #2945, xarray behaves the same as numpy. In #4612, xarray behaves differently from numpy.
Environment:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 4.12.14-lp150.12.82-default
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
libhdf5: 1.10.6
libnetcdf: 4.7.4
xarray: 0.16.1
pandas: 1.1.4
numpy: 1.19.4
scipy: 1.5.3
netCDF4: 1.5.4
pydap: None
h5netcdf: 0.8.1
h5py: 3.1.0
Nio: None
zarr: 2.5.0
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.7
cfgrib: None
iris: None
bottleneck: None
dask: 2.30.0
distributed: 2.30.1
matplotlib: 3.3.2
cartopy: 0.18.0
seaborn: None
numbagg: None
pint: None
setuptools: 49.6.0.post20201009
pip: 20.2.4
conda: installed
pytest: 6.1.2
IPython: 7.19.0
sphinx: 3.3.0
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 by running the minimal DataArray example and the equivalent NumPy assignment to confirm the differing behavior. Trace the xarray path used by DataArray integer indexing assignment, then add regression coverage for assigning numpy.nan to a uint8 array. Done means xarray raises ValueError instead of converting nan to an integer.
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
- Mostly clear
- Newbie friendliness
- 45/100