Skip to content

Comparison with masked array yields object-array with nans for masked values #1792

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

Open
gerritholl opened this issue Dec 19, 2017 · 3 comments
Labels

Comments

@gerritholl
Copy link
Contributor

Code Sample, a copy-pastable example if possible

$ cat mwe.py 
#!/usr/bin/env python3.6
import xarray
import numpy

da = xarray.DataArray(numpy.arange(5))
ma = numpy.ma.masked_array(numpy.arange(5), [True, False, False, False, True])
print(da>ma)
$ ./mwe.py 
<xarray.DataArray (dim_0: 5)>
array([nan, False, False, False, nan], dtype=object)
Dimensions without coordinates: dim_0

Problem description

A comparison between a DataArray and a masked_array results in an array with dtype object instead of an array with dtype bool. This is problematic, because code should be able to assume that x > y returns something with a bool dtype.

Expected Output

I would expect the masked array to be dropped (which it is) and an array to be returned equivalent to the comparison da>ma.data

<xarray.DataArray (dim_0: 5)>
array([False, False, False, False, False], dtype=bool)
Dimensions without coordinates: dim_0

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Linux OS-release: 2.6.32-696.6.3.el6.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8

xarray: 0.10.0+dev12.gf882a58
pandas: 0.21.0
numpy: 1.13.3
scipy: 1.0.0
netCDF4: 1.3.1
h5netcdf: None
Nio: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.16.0
matplotlib: 2.1.0
cartopy: None
seaborn: 0.8.1
setuptools: 38.2.4
pip: 9.0.1
conda: 4.3.16
pytest: 3.1.2
IPython: 6.1.0
sphinx: 1.6.2
None

@shoyer shoyer added the bug label Dec 30, 2017
@shoyer
Copy link
Member

shoyer commented Dec 30, 2017

Yes, I suppose we can consider this a bug. The right behavior would be to convert the masked array into an xarray object before the operation, e.g.,

In [14]: da > xarray.DataArray(ma)
Out[14]:
<xarray.DataArray (dim_0: 5)>
array([False, False, False, False, False], dtype=bool)
Dimensions without coordinates: dim_0

We currently don't have any masked array specific logic for arithmetic so this would require special case handling.

@gerritholl
Copy link
Contributor Author

See also: #2377.

@jhamman jhamman modified the milestones: 0.10.3, 0.11.1 Nov 7, 2018
gerritholl added a commit to pytroll/fogpy that referenced this issue Apr 24, 2019
Add a workaround for pydata/xarray#1792 ---
the mask was accidentally being turned into an object dtype array.
gerritholl added a commit to pytroll/fogpy that referenced this issue May 29, 2019
Add a workaround for pydata/xarray#1792 ---
the mask was accidentally being turned into an object dtype array.
gerritholl added a commit to pytroll/fogpy that referenced this issue May 29, 2019
Add a workaround for pydata/xarray#1792 ---
the mask was accidentally being turned into an object dtype array.
gerritholl added a commit to pytroll/fogpy that referenced this issue Jun 4, 2019
Add a workaround for pydata/xarray#1792 ---
the mask was accidentally being turned into an object dtype array.
gerritholl added a commit to pytroll/fogpy that referenced this issue Jun 18, 2019
Add a workaround for pydata/xarray#1792 ---
the mask was accidentally being turned into an object dtype array.
gerritholl added a commit to pytroll/fogpy that referenced this issue Jun 18, 2019
Add a workaround for pydata/xarray#1792 ---
the mask was accidentally being turned into an object dtype array.
@stale
Copy link

stale bot commented Oct 11, 2020

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Oct 11, 2020
@dcherian dcherian removed the stale label Oct 11, 2020
@dcherian dcherian removed this from the 0.11.1 milestone Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants