8000 Error when equating a masked and non-masked structured array · Issue #3840 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Error when equating a masked and non-masked structured array #3840

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

Closed
astrofrog opened this issue Sep 30, 2013 · 5 comments
Closed

Error when equating a masked and non-masked structured array #3840

astrofrog opened this issue Sep 30, 2013 · 5 comments

Comments

@astrofrog
Copy link
Contributor
astrofrog commented Sep 30, 2013

I see the following error with the latest version of Numpy (1.8.0rc1):

In [3]: x = np.ma.MaskedArray([(2,),(2,)], dtype = [('a', '<i8')])

In [4]: y = np.array([(2,), (2,)], dtype=[('a', '<i8')])

In [5]: x == y
Old (outdated) traceback
Out[5]: ---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-73b2790c1e5e> in <module>()
----> 1 x == y

/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/displayhook.py in __call__(self, result)
    245             self.start_displayhook()
    246             self.write_output_prompt()
--> 247             format_dict, md_dict = self.compute_format_data(result)
    248             self.write_format_data(format_dict, md_dict)
    249             self.update_user_ns(result)

/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/displayhook.py in compute_format_data(self, result)
    155 
    156         """
--> 157         return self.shell.display_formatter.format(result)
    158 
    159     def write_format_data(self, format_dict, md_dict=None):

/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/formatters.py in format(self, obj, include, exclude)
    150             md = None
    151             try:
--> 152                 data = formatter(obj)
    153             except:
    154                 # FIXME: log the exception

/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/formatters.py in __call__(self, obj)
    478                 type_pprinters=self.type_printers,
    479                 deferred_pprinters=self.deferred_printers)
--> 480             printer.pretty(obj)
    481             printer.flush()
    482             return stream.getvalue()

/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    361                             if isinstance(meth, collections.Callable):
    362                                 return meth(obj, self, cycle)
--> 363             return _default_pprint(obj, self, cycle)
    364         finally:
    365             self.end_group()

/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
    481     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
    482         # A user-provided repr.
--> 483         p.text(repr(obj))
    484         return
    485     p.begin_group(1, '<')

/Users/tom/Library/Python/3.3/lib/python/site-packages/numpy/ma/core.py in __repr__(self)
   3601         name = repr(self._data).split('(')[0]
   3602         parameters = dict(name=name, nlen=" " * len(name),
-> 3603                            data=str(self), mask=str(self._mask),
   3604                            fill=str(self.fill_value), dtype=str(self.dtype))
   3605         if self.dtype.names:

/Users/tom/Library/Python/3.3/lib/python/site-packages/numpy/ma/core.py in __str__(self)
   3585                 if names is None:
   3586                     res = self._data.astype("O")
-> 3587                     res[m] = f
   3588                 else:
   3589                     rdtype = _recursive_make_descr(self.dtype, "O")

IndexError: arrays used as indices must be of integer (or boolean) type

I think the correct behavior internally should be equivalent to:

In [6]: x == np.ma.MaskedArray(y)
Out[6]: 
masked_array(data = [True True],
             mask = [False False],
       fill_value = True)
@njsmith
Copy link
Member
njsmith commented Sep 30, 2013

For the record, I see the same behaviour on 1.7.1, so doesn't seem to be a
regression.

@eric-wieser
Copy link
Member

This is probably fixed in #8590

@eric-wieser
Copy link
Member
eric-wieser commented Sep 27, 2017

A variant of this bug persists in 1.14-dev:

In [10]: x == y
numpy\ma\core.py:3938: FutureWarning: elementwise == comparison failed and returning scalar instead; this will raise an error or perform elementwise comparison in the future.
  check = np.where(mask, compare(smask, omask), check)
Out[10]:
masked_array(data = [True True],
             mask = [False False],
       fill_value = (999999,))

Ultimately, the issue is that the return type of getmask doesn't make any sense for structured dtypes:

>>> np.ma.getmask(y)
np.False_

We're kind of stuck here though - we can't really do anything other than allocate a full mask array in getmask which is obviously not ideal either.

Pinging @ahaldane, since #8950 causes the warning here

9AF4
@seberg
Copy link
Member
seberg commented Dec 7, 2022

Still a bit similar. It now fails because the masks cannot be compared (because we don't allow comparing sturctured and non-structured dtypes at the moment). There, the problem is that the mask is nomask which is just np.False_.

@seberg
Copy link
Member
seberg commented Aug 9, 2024

Gonna close this, because this works now. That doesn't mean it makes a lot of sense. Because masked arrays have structured masks, and I dunno if it is right to consider a structured equality result as not-masked if there is any non-masked entry in the structured element.
(OTOH, maybe that is just how ma defined it...)

@seberg seberg closed this as completed Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0