From 9177d0b5776550e2fbb3b1c9a922832a6553f3e2 Mon Sep 17 00:00:00 2001 From: Allan Haldane Date: Mon, 8 May 2017 13:29:48 -0400 Subject: BUG: Preserve field order in join_by, avoids FutureWarning Fixes #8940 --- numpy/lib/tests/test_recfunctions.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 699a04716..0940d37b0 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -4,7 +4,7 @@ import numpy as np import numpy.ma as ma from numpy.ma.mrecords import MaskedRecords from numpy.ma.testutils import assert_equal -from numpy.testing import TestCase, run_module_suite, assert_ +from numpy.testing import TestCase, run_module_suite, assert_, assert_raises from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, find_duplicates, merge_arrays, append_fields, stack_arrays, join_by @@ -633,6 +633,19 @@ class TestJoinBy(TestCase): dtype=[('a', int), ('b', int), ('c', int), ('d', int)]) assert_equal(test, control) + def test_different_field_order(self): + # gh-8940 + a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u1')]) + b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')]) + # this should not give a FutureWarning: + j = join_by(['c', 'b'], a, b, jointype='inner', usemask=False) + assert_equal(j.dtype.names, ['b', 'c', 'a1', 'a2']) + + def test_duplicate_keys(self): + a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u1')]) + b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')]) + assert_raises(ValueError, join_by, ['a', 'b', 'b'], a, b) + class TestJoinBy2(TestCase): @classmethod -- cgit v1.2.1 From 64b0e54ce6ebf6bb16b9305f4d7f22b943c26b84 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 27 Jun 2017 18:49:47 +0100 Subject: TST: Verify that join_by doesn't choke on subdtypes --- numpy/lib/tests/test_recfunctions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 0940d37b0..e9cfa4993 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -588,6 +588,16 @@ class TestJoinBy(TestCase): dtype=[('a', int), ('b', int), ('c', int), ('d', int)]) + def test_join_subdtype(self): + # tests the bug in https://stackoverflow.com/q/44769632/102441 + from numpy.lib import recfunctions as rfn + foo = np.array([(1,)], + dtype=[('key', int)]) + bar = np.array([(1, np.array([1,2,3]))], + dtype=[('key', int), ('value', 'uint16', 3)]) + res = join_by('key', foo, bar) + assert_equal(res, bar.view(ma.MaskedArray)) + def test_outer_join(self): a, b = self.a, self.b -- cgit v1.2.1 From cd761d81b571525ac6c2cca36da6bd270bb8357d Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 1 Jul 2017 13:05:15 +0100 Subject: BUG: recfunctions.join_by fails for colliding values with different dtypes Fixes #9338 --- numpy/lib/tests/test_recfunctions.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index e9cfa4993..a5d15cb24 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -656,6 +656,19 @@ class TestJoinBy(TestCase): b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')]) assert_raises(ValueError, join_by, ['a', 'b', 'b'], a, b) + def test_same_name_different_dtypes(self): + # gh-9338 + a_dtype = np.dtype([('key', 'S10'), ('value', ' Date: Sat, 1 Jul 2017 14:25:21 +0100 Subject: BUG: recfunctions.join_by fails when key is a subdtype It seems that working with .descr is a generally terrible idea. Instead we introduce `get_fieldspec`, which returns a list of 2-tuples, encapsulating subdtypes. This also means that np.core.test_rational.rational survives a roundtrip - its .descr is 'V8', which ddoesn't survive --- numpy/lib/tests/test_recfunctions.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index a5d15cb24..50a85a08e 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -669,6 +669,20 @@ class TestJoinBy(TestCase): assert_equal(res.dtype, expected_dtype) + def test_subarray_key(self): + a_dtype = np.dtype([('pos', int, 3), ('f', ' Date: Sat, 1 Jul 2017 15:20:01 +0100 Subject: BUG: stack_arrays fails for subdtypes Again, fixed by not using descr --- numpy/lib/tests/test_recfunctions.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 50a85a08e..799d7cd8e 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -546,6 +546,35 @@ class TestStackArrays(TestCase): assert_equal(test, control) assert_equal(test.mask, control.mask) + def test_subdtype(self): + z = np.array([ + ('A', 1), ('B', 2) + ], dtype=[('A', '|S3'), ('B', float, (1,))]) + zz = np.array([ + ('a', [10.], 100.), ('b', [20.], 200.), ('c', [30.], 300.) + ], dtype=[('A', '|S3'), ('B', float, (1,)), ('C', float)]) + + res = stack_arrays((z, zz)) + expected = ma.array( + data=[ + (b'A', [1.0], 0), + (b'B', [2.0], 0), + (b'a', [10.0], 100.0), + (b'b', [20.0], 200.0), + (b'c', [30.0], 300.0)], + mask=[ + (False, [False], True), + (False, [False], True), + (False, [False], False), + (False, [False], False), + (False, [False], False) + ], + dtype=zz.dtype + ) + assert_equal(res.dtype, expected.dtype) + assert_equal(res, expected) + assert_equal(res.mask, expected.mask) + class TestJoinBy(TestCase): def setUp(self): -- cgit v1.2.1 From e6e0de242ba6f22e0bc3792db4dc64d6b6053d16 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 1 Jul 2017 15:30:35 +0100 Subject: TST: Add failing test for mismatch of key type --- numpy/lib/tests/test_recfunctions.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 799d7cd8e..36635ec43 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -4,7 +4,9 @@ import numpy as np import numpy.ma as ma from numpy.ma.mrecords import MaskedRecords from numpy.ma.testutils import assert_equal -from numpy.testing import TestCase, run_module_suite, assert_, assert_raises +from numpy.testing import ( + TestCase, run_module_suite, assert_, assert_raises, dec +) from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, find_duplicates, merge_arrays, append_fields, stack_arrays, join_by @@ -685,6 +687,19 @@ class TestJoinBy(TestCase): b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')]) assert_raises(ValueError, join_by, ['a', 'b', 'b'], a, b) + @dec.knownfailureif(True) + def test_same_name_different_dtypes_key(self): + a_dtype = np.dtype([('key', 'S5'), ('value', ' Date: Sat, 1 Jul 2017 20:03:03 +0100 Subject: TST: join_by now works for dtypes with padding too Once again, thanks to not using .descr --- numpy/lib/tests/test_recfunctions.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 36635ec43..7cf93d67f 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -727,6 +727,22 @@ class TestJoinBy(TestCase): assert_equal(res.dtype, expected_dtype) assert_equal(res, expected) + def test_padded_dtype(self): + dt = np.dtype('i1,f4', align=True) + dt.names = ('k', 'v') + assert_(len(dt.descr), 3) # padding field is inserted + + a = np.array([(1, 3), (3, 2)], dt) + b = np.array([(1, 1), (2, 2)], dt) + res = join_by('k', a, b) + + # no padding fields remain + expected_dtype = np.dtype([ + ('k', 'i1'), ('v1', 'f4'), ('v2', 'f4') + ]) + + assert_equal(res.dtype, expected_dtype) + class TestJoinBy2(TestCase): @classmethod -- cgit v1.2.1 From 8eee1b06b330f6ff23fd3604444ba20a8d2d6416 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Mon, 17 Jul 2017 17:51:33 -0600 Subject: TST: Remove unittest dependencies in numpy/lib/tests. --- numpy/lib/tests/test_recfunctions.py | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 7cf93d67f..bc9f8d7b6 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -5,8 +5,8 @@ import numpy.ma as ma from numpy.ma.mrecords import MaskedRecords from numpy.ma.testutils import assert_equal from numpy.testing import ( - TestCase, run_module_suite, assert_, assert_raises, dec -) + run_module_suite, assert_, assert_raises, dec + ) from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, find_duplicates, merge_arrays, append_fields, stack_arrays, join_by @@ -16,10 +16,10 @@ get_names_flat = np.lib.recfunctions.get_names_flat zip_descr = np.lib.recfunctions.zip_descr -class TestRecFunctions(TestCase): +class TestRecFunctions(object): # Misc tests - def setUp(self): + def setup(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array([('A', 1.), ('B', 2.)], @@ -193,7 +193,7 @@ class TestRecFunctions(TestCase): assert_equal(test[0], a[test[-1]]) -class TestRecursiveFillFields(TestCase): +class TestRecursiveFillFields(object): # Test recursive_fill_fields. def test_simple_flexible(self): # Test recursive_fill_fields on flexible-array @@ -216,10 +216,10 @@ class TestRecursiveFillFields(TestCase): assert_equal(test, control) -class TestMergeArrays(TestCase): +class TestMergeArrays(object): # Test merge_arrays - def setUp(self): + def setup(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array( @@ -349,10 +349,10 @@ class TestMergeArrays(TestCase): assert_equal(test, control) -class TestAppendFields(TestCase): +class TestAppendFields(object): # Test append_fields - def setUp(self): + def setup(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array( @@ -403,9 +403,9 @@ class TestAppendFields(TestCase): assert_equal(test, control) -class TestStackArrays(TestCase): +class TestStackArrays(object): # Test stack_arrays - def setUp(self): + def setup(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array( @@ -419,11 +419,11 @@ class TestStackArrays(TestCase): (_, x, _, _) = self.data test = stack_arrays((x,)) assert_equal(test, x) - self.assertTrue(test is x) + assert_(test is x) test = stack_arrays(x) assert_equal(test, x) - self.assertTrue(test is x) + assert_(test is x) def test_unnamed_fields(self): # Tests combinations of arrays w/o named fields @@ -578,8 +578,8 @@ class TestStackArrays(TestCase): assert_equal(res.mask, expected.mask) -class TestJoinBy(TestCase): - def setUp(self): +class TestJoinBy(object): + def setup(self): self.a = np.array(list(zip(np.arange(10), np.arange(50, 60), np.arange(100, 110))), dtype=[('a', int), ('b', int), ('c', int)]) @@ -744,9 +744,9 @@ class TestJoinBy(TestCase): assert_equal(res.dtype, expected_dtype) -class TestJoinBy2(TestCase): +class TestJoinBy2(object): @classmethod - def setUp(cls): + def setup(cls): cls.a = np.array(list(zip(np.arange(10), np.arange(50, 60), np.arange(100, 110))), dtype=[('a', int), ('b', int), ('c', int)]) @@ -770,8 +770,8 @@ class TestJoinBy2(TestCase): assert_equal(test, control) def test_no_postfix(self): - self.assertRaises(ValueError, join_by, 'a', self.a, self.b, - r1postfix='', r2postfix='') + assert_raises(ValueError, join_by, 'a', self.a, self.b, + r1postfix='', r2postfix='') def test_no_r2postfix(self): # Basic test of join_by no_r2postfix @@ -809,13 +809,13 @@ class TestJoinBy2(TestCase): assert_equal(test.dtype, control.dtype) assert_equal(test, control) -class TestAppendFieldsObj(TestCase): +class TestAppendFieldsObj(object): """ Test append_fields with arrays containing objects """ # https://github.com/numpy/numpy/issues/2346 - def setUp(self): + def setup(self): from datetime import date self.data = dict(obj=date(2000, 1, 1)) -- cgit v1.2.1 From 7e5a41de9fab731e27a761c01302a0a93e2d1070 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 25 Mar 2018 12:34:16 -0600 Subject: TST: Switch to using pytest markers Use standard pytest markers everywhere in the numpy tests. At this point there should be no nose dependency. However, nose is required to test the legacy decorators if so desired. At this point, numpy test cannot be run in the way with runtests, rather installed numpy can be tested with `pytest --pyargs numpy` as long as that is not run from the repo. Run it from the tools directory or some such. --- numpy/lib/tests/test_recfunctions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index bc9f8d7b6..e1b62d46a 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -1,11 +1,13 @@ from __future__ import division, absolute_import, print_function +import pytest + import numpy as np import numpy.ma as ma from numpy.ma.mrecords import MaskedRecords from numpy.ma.testutils import assert_equal from numpy.testing import ( - run_module_suite, assert_, assert_raises, dec + run_module_suite, assert_, assert_raises, ) from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, @@ -687,7 +689,7 @@ class TestJoinBy(object): b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')]) assert_raises(ValueError, join_by, ['a', 'b', 'b'], a, b) - @dec.knownfailureif(True) + @pytest.mark.xfail(reason="See comment at gh-9343") def test_same_name_different_dtypes_key(self): a_dtype = np.dtype([('key', 'S5'), ('value', ' Date: Wed, 4 Apr 2018 10:33:07 -0600 Subject: MAINT: Remove all uses of run_module_suite. That function is nose specific and has not worked since `__init__` files were added to the tests directories. --- numpy/lib/tests/test_recfunctions.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index e1b62d46a..219ae24fa 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -6,9 +6,7 @@ import numpy as np import numpy.ma as ma from numpy.ma.mrecords import MaskedRecords from numpy.ma.testutils import assert_equal -from numpy.testing import ( - run_module_suite, assert_, assert_raises, - ) +from numpy.testing import assert_, assert_raises from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, find_duplicates, merge_arrays, append_fields, stack_arrays, join_by @@ -831,6 +829,3 @@ class TestAppendFieldsObj(object): control = np.array([(obj, 1.0, 10), (obj, 2.0, 20)], dtype=[('A', object), ('B', float), ('C', int)]) assert_equal(test, control) - -if __name__ == '__main__': - run_module_suite() -- cgit v1.2.1 From e08eced7990fbdcecb2bd81d3fc736f69bad6dfd Mon Sep 17 00:00:00 2001 From: Allan Haldane Date: Sun, 10 Jun 2018 21:54:21 -0400 Subject: MAINT: push back multifield copy->view changes to 1.16 --- numpy/lib/tests/test_recfunctions.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'numpy/lib/tests/test_recfunctions.py') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 219ae24fa..d4828bc1f 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -9,8 +9,8 @@ from numpy.ma.testutils import assert_equal from numpy.testing import assert_, assert_raises from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, - find_duplicates, merge_arrays, append_fields, stack_arrays, join_by - ) + find_duplicates, merge_arrays, append_fields, stack_arrays, join_by, + repack_fields) get_names = np.lib.recfunctions.get_names get_names_flat = np.lib.recfunctions.get_names_flat zip_descr = np.lib.recfunctions.zip_descr @@ -192,6 +192,18 @@ class TestRecFunctions(object): assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) + def test_repack_fields(self): + dt = np.dtype('u1,f4,i8', align=True) + a = np.zeros(2, dtype=dt) + + assert_equal(repack_fields(dt), np.dtype('u1,f4,i8')) + assert_equal(repack_fields(a).itemsize, 13) + assert_equal(repack_fields(repack_fields(dt), align=True), dt) + + # make sure type is preserved + dt = np.dtype((np.record, dt)) + assert_(repack_fields(dt).type is np.record) + class TestRecursiveFillFields(object): # Test recursive_fill_fields. -- cgit v1.2.1