[Numpy-svn] r5253 - in trunk/numpy/ma: . tests

numpy-svn@scip... numpy-svn@scip...
Wed Jun 4 10:55:15 CDT 2008


Author: pierregm
Date: 2008-06-04 10:54:28 -0500 (Wed, 04 Jun 2008)
New Revision: 5253

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
simplified MaskedArray.__setitem__ to fix setting object-ndarray elements

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-06-03 23:41:20 UTC (rev 5252)
+++ trunk/numpy/ma/core.py	2008-06-04 15:54:28 UTC (rev 5253)
@@ -1413,25 +1413,26 @@
             self._sharedmask = False
             return
         #....
-        dval = np.array(value, copy=False, dtype=self.dtype)
-        valmask = getmask(value)
+#        dval = np.array(value, copy=False, dtype=self.dtype)
+        dval = value
+        mval = getmask(value)
         if self._mask is nomask:
             # Set the data, then the mask
             ndarray.__setitem__(self._data,indx,dval)
-            if valmask is not nomask:
+            if mval is not nomask:
                 self._mask = np.zeros(self.shape, dtype=MaskType)
-                self._mask[indx] = valmask
+                self._mask[indx] = mval
         elif not self._hardmask:
             # Unshare the mask if necessary to avoid propagation
             self.unshare_mask()
             # Set the data, then the mask
-            ndarray.__setitem__(self._data,indx,dval)
-            self._mask[indx] = valmask
+            ndarray.__setitem__(self._data, indx, dval)
+            ndarray.__setitem__(self._mask, indx, mval)
         elif hasattr(indx, 'dtype') and (indx.dtype==bool_):
             indx = indx * umath.logical_not(self._mask)
-            ndarray.__setitem__(self._data,indx,dval)
+            ndarray.__setitem__(self._data, indx, dval)
         else:
-            mindx = mask_or(self._mask[indx], valmask, copy=True)
+            mindx = mask_or(self._mask[indx], mval, copy=True)
             dindx = self._data[indx]
             if dindx.size > 1:
                 dindx[~mindx] = dval
@@ -2469,7 +2470,7 @@
             idx_l = idx.tolist()
             tmp_mask = self._mask[idx_l].flat
             tmp_data = self._data[idx_l].flat
-            self.flat = tmp_data
+            self._data.flat = tmp_data
             self._mask.flat = tmp_mask
         return
 

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-06-03 23:41:20 UTC (rev 5252)
+++ trunk/numpy/ma/tests/test_core.py	2008-06-04 15:54:28 UTC (rev 5253)
@@ -631,6 +631,19 @@
         ctest = masked_where(btest,atest)
         assert_equal(atest,ctest)
     #........................
+    def test_set_oddities(self):
+        """Tests setting elements with object""" 
+        a = empty(1,dtype=object)
+        x = (1,2,3,4,5)
+        a[0] = x
+        assert_equal(a[0], x)
+        assert(a[0] is x)
+        #
+        import datetime
+        dt = datetime.datetime.now()
+        a[0] = dt
+        assert(a[0] is dt)
+    #........................
     def test_maskingfunctions(self):
         "Tests masking functions."
         x = array([1.,2.,3.,4.,5.])



More information about the Numpy-svn mailing list