How can I mask elements of an array of records in Numpy?

I understand how to create a mask in a mask, and I would like to use masking in an array of records so that I can access this data using named attributes. Masking seems "lost" when I create an array of records 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 the record, the data is not masked:

 >>> r.date[0] 0 

Unlike the original array:

 >>> data['date'][0] masked_array(data = --, mask = True, fill_value = 1e+20) fill_value = 1e+20) 

What can I do? Does the array of records support masking? Browsing on the Internet I saw several code examples that seemed to offer otherwise, but that was not very clear. Hopefully I get a good answer here.

+6
source share
1 answer

I did not find much documentation on numpy.ma.mrecords.MaskedRecords other than a brief mention here . You can find some examples of how to use it while studying the unit tests that come with numpy. ( /usr/lib/python2.6/dist-packages/numpy/ma/tests/test_mrecords.py .. /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')]), mask=[i<10 for i in range(30)]) r = data.view(mrecords.mrecarray) print(r.date[0]) # -- 
+3
source

Source: https://habr.com/ru/post/896050/


All Articles