From fa56437c4eb4ca0c2a97597700a3fc6ad36963fa Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sun, 19 Feb 2017 14:07:03 +0000 Subject: BUG: Fix double-wrapping of object scalars Fixes #8642 --- numpy/lib/shape_base.py | 4 +++- numpy/lib/tests/test_shape_base.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index 58e13533b..81d8d9d17 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -105,8 +105,10 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs): in_dims = list(range(nd)) inarr_view = transpose(arr, in_dims[:axis] + in_dims[axis+1:] + [axis]) - # compute indices for the iteration axes + # compute indices for the iteration axes, and append a trailing ellipsis to + # prevent 0d arrays decaying to scalars, which fixes gh-8642 inds = ndindex(inarr_view.shape[:-1]) + inds = (ind + (Ellipsis,) for ind in inds) # invoke the function on the first item try: diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 8bdf3d3da..4d06001f4 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -159,6 +159,21 @@ class TestApplyAlongAxis(TestCase): assert_equal(actual, np.ones(10)) assert_raises(ValueError, np.apply_along_axis, empty_to_1, 0, a) + def test_with_iterable_object(self): + # from issue 5248 + d = np.array([ + [set([1, 11]), set([2, 22]), set([3, 33])], + [set([4, 44]), set([5, 55]), set([6, 66])] + ]) + actual = np.apply_along_axis(lambda a: set.union(*a), 0, d) + expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}]) + + assert_equal(actual, expected) + + # issue 8642 - assert_equal doesn't detect this! + for i in np.ndindex(actual.shape): + assert_equal(type(actual[i]), type(expected[i])) + class TestApplyOverAxes(TestCase): def test_simple(self): -- cgit v1.2.1