question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

masked arrays with subfields throws ValueError on accessing array elements

See original GitHub issue

Consider the following program:

import numpy as np
import numpy.ma as ma
if __name__ == '__main__':
    # works
    Y = ma.array(np.array([(1,2)], dtype=[('',int),('',float)]), mask=[(True, False)])
    print Y[0]

    # breaks
    Y = ma.array(np.array([(1,[2,3])], dtype=[('',int),('',float,(2,))]), mask=[(True, (False, True))])
    print Y[0]

The 2nd construction of the array Y tries to create a numpy array from the mask, which doesn’t quite work:

(venv)stephentu@fender sandbox3$ python -m pdb test.py 
> /home/stephentu/sandbox3/test.py(1)<module>()
-> import numpy as np
(Pdb) r
(--, 2.0)
--Return--
> /home/stephentu/sandbox3/test.py(11)<module>()->None
-> print Y[0]
(Pdb) 
ValueError: 'setting an array element with a sequence.'
> <string>(1)<module>()->None
(Pdb) 
--Return--
> <string>(1)<module>()->None
(Pdb) 
Traceback (most recent call last):
  File "/usr/lib64/python2.7/pdb.py", line 1314, in main
    pdb._runscript(mainpyfile)
  File "/usr/lib64/python2.7/pdb.py", line 1233, in _runscript
    self.run(statement)
  File "/usr/lib64/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "test.py", line 11, in <module>
    print Y[0]
  File "/usr/lib/python2.7/site-packages/numpy/ma/core.py", line 5614, in __str__
    if (not any(m)):
  File "/usr/lib/python2.7/site-packages/numpy/ma/core.py", line 6020, in __call__
    return method(MaskedArray(a), *args, **params)
  File "/usr/lib/python2.7/site-packages/numpy/ma/core.py", line 2637, in __new__
    _data = np.array(data, dtype=dtype, copy=copy, subok=True, ndmin=ndmin)
ValueError: setting an array element with a sequence.
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/lib/python2.7/site-packages/numpy/ma/core.py(2637)__new__()
-> _data = np.array(data, dtype=dtype, copy=copy, subok=True, ndmin=ndmin)
(Pdb) p data
(True, array([False,  True], dtype=bool))

Is there a workaround for the time being? Thanks!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
argriffingcommented, Aug 4, 2014

This happens because you are trying to do two weird things with numpy at the same time – subfields and masked arrays. Each weird thing you can do with numpy is probably tested, but in general I don’t think interactions are tested.

0reactions
eric-wiesercommented, Feb 24, 2017

Works on master too. But there’s still a bug in Y[0,...], which I’ve moved to #8684

Read more comments on GitHub >

github_iconTop Results From Across the Web

python 3 scatter plot gives "ValueError: Masked arrays must ...
matplotlib - python 3 scatter plot gives "ValueError: Masked arrays must be 1-D" even though i am not using any masked array -...
Read more >
Missing data: masked arrays
Suppose you are using a library that reads a file (e.g., netCDF) and returns the results as a masked array. Or, perhaps it...
Read more >
Masked arrays — NumPy v1.24 Manual
Masked arrays are arrays that may have missing or invalid entries. The numpy.ma module provides a nearly work-alike replacement for numpy that supports...
Read more >
Fluent NumPy. Let's uncover the practical details of… - Medium
Iterating over Arrays: Using nditer Iterator · Masked Arrays ... NumPy generally returns elements of arrays as array scalars (a scalar with ...
Read more >
Valueerror Setting an Array Element With a Sequence.: Fix It ...
The valueerror setting an array element with a sequence. problem is fixed by equaling the length on both arrays. Follow these steps to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found