8000 Merge pull request #8643 from eric-wieser/fix-8642 · numpy/numpy@f368286 · GitHub
[go: up one dir, main page]

Skip to content

Commit f368286

Browse files
authored
Merge pull request #8643 from eric-wieser/fix-8642
BUG: Fix double-wrapping of object scalars
2 parents 8b6b1bc + fa56437 commit f368286

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

numpy/lib/shape_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs):
103103
in_dims = list(range(nd))
104104
inarr_view = transpose(arr, in_dims[:axis] + in_dims[axis+1:] + [axis])
105105

106-
# compute indices for the iteration axes
106+
# compute indices for the iteration axes, and append a trailing ellipsis to
107+
# prevent 0d arrays decaying to scalars, which fixes gh-8642
107108
inds = ndindex(inarr_view.shape[:-1])
109+
inds = (ind + (Ellipsis,) for ind in inds)
108110

109111
# invoke the function on the first item
110112
try:

numpy/lib/tests/test_shape_base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ def empty_to_1(x):
159159
assert_equal(actual, np.ones(10))
160160
assert_raises(ValueError, np.apply_along_axis, empty_to_1, 0, a)
161161

162+
def test_with_iterable_object(self):
163+
# from issue 5248
164+
d = np.array([
165+
[set([1, 11]), set([2, 22]), set([3, 33])],
166+
[set([4, 44]), set([5, 55]), set([6, 66])]
167+
])
168+
actual = np.apply_along_axis(lambda a: set.union(*a), 0, d)
169+
expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}])
170+
171+
assert_equal(actual, expected)
172+
173+
# issue 8642 - assert_equal doesn't detect this!
174+
for i in np.ndindex(actual.shape):
175+
assert_equal(type(actual[i]), type(expected[i]))
176+
162177

163178
class TestApplyOverAxes(TestCase):
164179
def test_simple(self):

0 commit comments

Comments
 (0)
0