# ¿Cómo puedo enmascarar elementos de una matriz de registros en Numpy?

I understand how to create a masked array, and I would like to use masking in a record array so that I can access this data using named attributes. The masking seems to be "lost" when I create a record array from a masked array:

``````>>> data = np.ma.array(np.ma.zeros(30, dtype=[('date', '|O4'), ('price', '<f8')]),mask=[i<10 for i in range(30)])
>>> data
masked_array(data = [(--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --)
(--, --) (--, --) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0)
(0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0)],
mask = [(True, True) (True, True) (True, True) (True, True) (True, True)
(True, True) (True, True) (True, True) (True, True) (True, True)
(False, False) (False, False) (False, False) (False, False) (False, False)
(False, False) (False, False) (False, False) (False, False) (False, False)
(False, False) (False, False) (False, False) (False, False) (False, False)
(False, False) (False, False) (False, False) (False, False) (False, False)],
fill_value = ('?', 1e+20),
dtype = [('date', '|O4'), ('price', '<f8')])

>>> r = data.view(np.recarray)
>>> r
rec.array([(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0),
(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0),
(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0),
(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0),
(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
dtype=[('date', '|O4'), ('price', '<f8')])
``````

When I access a record the data is not masked:

``````>>> r.date[0]
0
``````

Unlike in the original array:

``````>>> data['date'][0]
fill_value = 1e+20)

fill_value = 1e+20)
``````

What can I do? Does the record array not support masking? Browsing on the web I have seen some code examples that seem to suggest otherwise, but it wasn't very clear. Hoping I can get a good answer here.

preguntado el 27 de agosto de 11 a las 21:08

## 1 Respuestas

I haven't found much documentation on numpy.ma.mrecords.MaskedRecords, except for a brief mention aquí. You can find some examples on how to use it by studying the unit tests that come with numpy. (e.g. `/usr/lib/python2.6/dist-packages/numpy/ma/tests/test_mrecords.py`).

``````import numpy as np
import numpy.ma.mrecords as mrecords

data = np.ma.array(
np.ma.zeros(30, dtype=[('date', '|O4'), ('price', '<f8')]),

r = data.view(mrecords.mrecarray)

print(r.date[0])
# --
``````

Respondido 28 ago 11, 02:08

Thanks, I didn't know about numpy.ma.mrecords. - Nate Reed

I don't suppose there is a way to mask individual fields? My use case is to add a new derived field called "100-day-high" (or something similar) so I would want to mask this field for the first 100 records. - Nate Reed

@Nate Reed: I think it is possible. Have you tried something like `data.mask['price'][data['date']<...]=True` ? - unutbu

Yes, either of these works: data.mask[0]['price'] = True, or data.mask['price'][0] = True. Thanks! - Nate Reed

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.