Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Problem with views on masked structured arrays #10483

Closed
@neishm

Description

@neishm

Create a view on a structured array (e.g. selecting a list of columns) causes problems with comparisons when the array is masked.

Example

Create a view on a masked structured array:

import numpy as np
data = np.ma.array([
  ('Mon', 'Sunny',  30, 20),
  ('Tue', 'Cloudy', 28, 17),
  ('Wed', 'Rain',   25, 17),
  ('Thu', 'Sunny',  29, 18),
  ('Fri', 'Sunny',  30, 20),
], dtype=[('day','|S3'),('weather','|S10'),('high','i4'),('low','i4')])
data2 = data[['day','high']]
print (data2)
[('Mon', 30) ('Tue', 28) ('Wed', 25) ('Thu', 29) ('Fri', 30)]

Try to do a comparison on the array:

print (data2[1:] == data2[:-1])
Traceback (most recent call last):
  File "test_numpy14.py", line 12, in <module>
    print (data2[1:] == data2[:-1])
  File "XXX/lib/python2.7/site-packages/numpy/ma/core.py", line 4033, in __eq__
    return self._comparison(other, operator.eq)
  File "XXX/lib/python2.7/site-packages/numpy/ma/core.py", line 3993, in _comparison
    sdata = sbroadcast.filled(odata)
  File "XXX/lib/python2.7/site-packages/numpy/ma/core.py", line 3709, in filled
    fill_value = _check_fill_value(fill_value, self.dtype)
  File "XXX/lib/python2.7/site-packages/numpy/ma/core.py", line 459, in _check_fill_value
    raise ValueError(err_msg % (fill_value, fdtype))
ValueError: Unable to transform [('Mon', 30) ('Tue', 28) ('Wed', 25) ('Thu', 29)] to dtype [('day', '|S3'), ('', '|V10'), ('high', '<i4'), ('', '|V4')]

A similar problem happens when calling numpy.ma.unique on the view. The problem persists even after copying the data (e.g. data2 = data2.copy())

I am using numpy 1.14.0 with Python 2.7.6. The problem does not appear in numpy 1.13.3, or when using a regular (non-masked) structured array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions