[Numpy-svn] r8626 - in trunk/numpy/core: . tests

numpy-svn@scip... numpy-svn@scip...
Sat Aug 14 08:44:49 CDT 2010


Author: rgommers
Date: 2010-08-14 08:44:49 -0500 (Sat, 14 Aug 2010)
New Revision: 8626

Modified:
   trunk/numpy/core/_internal.py
   trunk/numpy/core/tests/test_records.py
Log:
DEP: deprecate behavior for out-of-order field indexing of recarrays. See #1431.

Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py	2010-08-14 13:43:39 UTC (rev 8625)
+++ trunk/numpy/core/_internal.py	2010-08-14 13:44:49 UTC (rev 8626)
@@ -3,6 +3,7 @@
 
 import re
 import sys
+import warnings
 
 from numpy.compat import asbytes, bytes
 
@@ -335,18 +336,24 @@
     from multiarray import empty, dtype
     dt = ary.dtype
     new_dtype = [(name, dt[name]) for name in dt.names if name in fields]
+    future_dtype = [(name, dt[name]) for name in fields if name in dt.names]
+    if not new_dtype == future_dtype:
+        depdoc = "Out of order field selection on recarrays currently returns \
+fields in order. This behavior is deprecated in numpy 1.5 and will change in \
+2.0. See ticket #1431."
+        warnings.warn(depdoc, DeprecationWarning)
     if ary.flags.f_contiguous:
         order = 'F'
     else:
         order = 'C'
 
-    newarray = empty(ary.shape, dtype=new_dtype, order=order) 
-   
+    newarray = empty(ary.shape, dtype=new_dtype, order=order)
+
     for name in fields:
         newarray[name] = ary[name]
 
     return newarray
-    
+
 # Given a string containing a PEP 3118 format specifier,
 # construct a Numpy dtype
 

Modified: trunk/numpy/core/tests/test_records.py
===================================================================
--- trunk/numpy/core/tests/test_records.py	2010-08-14 13:43:39 UTC (rev 8625)
+++ trunk/numpy/core/tests/test_records.py	2010-08-14 13:44:49 UTC (rev 8626)
@@ -3,6 +3,9 @@
 from numpy.testing import *
 from numpy.compat import asbytes, asunicode
 
+import warnings
+
+
 class TestFromrecords(TestCase):
     def test_fromrecords(self):
         r = np.rec.fromrecords([[456, 'dbe', 1.2], [2, 'de', 1.3]],
@@ -131,7 +134,18 @@
             x[0].col5 = 1
         self.assertRaises(AttributeError, assign_invalid_column, a)
 
+    def test_out_of_order_fields(self):
+        """Ticket #1431. Current behavior deprecated in numpy 1.5"""
+        x = self.data[['col1', 'col2']]
+        y = self.data[['col2', 'col1']]
+        # make sure change is applied in 1.6/2.0
+        if np.version.short_version[:3] == '1.5':
+            assert_array_equal(x, y)
+        elif float(np.version.short_version[:3]) >= 1.6 and np.version.release:
+            assert_(y[0][0] == 4)
 
+warnings.filterwarnings('ignore', message="Out of order field selection on recarrays")
+
 def test_find_duplicate():
     l1 = [1, 2, 3, 4, 5, 6]
     assert np.rec.find_duplicate(l1) == []



More information about the Numpy-svn mailing list