summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2008-07-18 02:02:19 +0000
committerpierregm <pierregm@localhost>2008-07-18 02:02:19 +0000
commitf1104f880af4443df3a412ffdbbd151d03c1e932 (patch)
tree9c10d0e6c4bf2013a9d7046c11f8f1063257054b /numpy
parentab911fc322d50ce9928b211954857cc56e7a1595 (diff)
downloadnumpy-f1104f880af4443df3a412ffdbbd151d03c1e932.tar.gz
* added .torecords(), to convert a masked array to a flexible array with fields '_data' and '_mask'
Diffstat (limited to 'numpy')
-rw-r--r--numpy/ma/core.py25
-rw-r--r--numpy/ma/tests/test_core.py32
2 files changed, 57 insertions, 0 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 6de5e7ee4..f1e652c5c 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -3087,6 +3087,31 @@ masked_%(name)s(data = %(data)s,
def tofile(self, fid, sep="", format="%s"):
raise NotImplementedError("Not implemented yet, sorry...")
+ def torecords(self):
+ """Transforms a masked array into a flexible-type array with two fields:
+ * the ``_data`` field stores the ``_data`` part of the array;
+ * the ``_mask`` field stores the ``_mask`` part of the array;
+
+ Warnings
+ --------
+ A side-effect of transforming a masked array into a flexible ndarray is
+ that metainformation (``fill_value``, ...) will be lost.
+
+ """
+ # Get the basic dtype ....
+ ddtype = self.dtype
+ # Make sure we have a mask
+ _mask = self._mask
+ if _mask is None:
+ _mask = make_mask_none(self.shape, ddtype)
+ # And get its dtype
+ mdtype = self._mask.dtype
+ #
+ record = np.ndarray(shape=self.shape,
+ dtype=[('_data',ddtype),('_mask',mdtype)])
+ record['_data'] = self._data
+ record['_mask'] = self._mask
+ return record
#--------------------------------------------
# Pickling
def __getstate__(self):
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index e480d139a..cb27e1d90 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -1650,6 +1650,38 @@ class TestMaskedArrayMethods(TestCase):
assert_equal(x.tolist(), [(1,1.1,'one'),(2,2.2,'two'),(None,None,None)])
+ def test_torecords(self):
+ "Test the conversion to records"
+ data = arange(10)
+ record = data.torecords()
+ assert_equal(record['_data'], data._data)
+ assert_equal(record['_mask'], data._mask)
+ #
+ data[[0,1,2,-1]] = masked
+ record = data.torecords()
+ assert_equal(record['_data'], data._data)
+ assert_equal(record['_mask'], data._mask)
+ #
+ ndtype = [('i',int), ('s','|S3'), ('f',float)]
+ data = array([(i,s,f) for (i,s,f) in zip(np.arange(10),
+ 'ABCDEFGHIJKLM',
+ np.random.rand(10))],
+ dtype=ndtype)
+ data[[0,1,2,-1]] = masked
+ record = data.torecords()
+ assert_equal(record['_data'], data._data)
+ assert_equal(record['_mask'], data._mask)
+ #
+ ndtype = np.dtype("int, (2,3)float, float")
+ data = array([(i,f,ff) for (i,f,ff) in zip(np.arange(10),
+ np.random.rand(10),
+ np.random.rand(10))],
+ dtype=ndtype)
+ data[[0,1,2,-1]] = masked
+ record = data.torecords()
+ assert_equal(record['_data'], data._data)
+ assert_equal(record['_mask'], data._mask)
+
#------------------------------------------------------------------------------