Skip to content

Commit

Permalink
DEP: (backport of r8626) deprecate behavior for out-of-order field in…
Browse files Browse the repository at this point in the history
…dexing of recarrays. See numpy#1431.
  • Loading branch information
rgommers committed Aug 14, 2010
1 parent 7ecf7c4 commit 98da149
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import re
import sys
import warnings

from numpy.compat import asbytes, bytes

Expand Down Expand Up @@ -306,18 +307,24 @@ def _index_fields(ary, fields):
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

Expand Down
14 changes: 14 additions & 0 deletions numpy/core/tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand Down Expand Up @@ -131,6 +134,17 @@ def assign_invalid_column(x):
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]
Expand Down

0 comments on commit 98da149

Please sign in to comment.