<p dir="auto">Under some circumstances, <code class="notranslate">trace</code> returns a scalar on a 3-D masked array, even though it should return a vector.  It appears to be related to the mask, as illustrated below.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [326]: Z = numpy.ma.empty((2, 2, 2), dtype=&quot;&lt;f4&quot;)

In [327]: trace(Z, 1, 2)
Out[327]: 
masked_array(data = [ 0.  0.],
             mask = False,
       fill_value = 1e+20)

In [328]: Z.mask = zeros_like(Z)

In [329]: trace(Z, 1, 2)
Out[329]: 0.0    # oops, that should have been [0., 0.]

In [330]: trace(Z.data, 1, 2)
Out[330]: array([ 0.,  0.], dtype=float32)"><pre class="notranslate"><code class="notranslate">In [326]: Z = numpy.ma.empty((2, 2, 2), dtype="&lt;f4")

In [327]: trace(Z, 1, 2)
Out[327]: 
masked_array(data = [ 0.  0.],
             mask = False,
       fill_value = 1e+20)

In [328]: Z.mask = zeros_like(Z)

In [329]: trace(Z, 1, 2)
Out[329]: 0.0    # oops, that should have been [0., 0.]

In [330]: trace(Z.data, 1, 2)
Out[330]: array([ 0.,  0.], dtype=float32)
</code></pre></div>
<p dir="auto">When <code class="notranslate">column_stack</code>ing two arrays, the dtype of the result is the dtype of the input arrays (if they are are the same). However, if the endianness of the input array is not the system endianness, it is not respected.</p>
<p dir="auto">MWE:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy

a = numpy.array([1, 2, 3], dtype=&quot;i4&quot;)
b = numpy.array([1, 2, 3], dtype=&quot;i4&quot;)
c = numpy.column_stack([a, b])
print(a.dtype)  # int32   okay
print(b.dtype)  # int32   okay
print(c.dtype)  # int32   okay
print()

a = numpy.array([1, 2, 3], dtype=&quot;=i4&quot;)
b = numpy.array([1, 2, 3], dtype=&quot;=i4&quot;)
c = numpy.column_stack([a, b])
print(a.dtype)  # int32   okay
print(b.dtype)  # int32   okay
print(c.dtype)  # int32   okay
print()

a = numpy.array([1, 2, 3], dtype=&quot;&lt;i4&quot;)
b = numpy.array([1, 2, 3], dtype=&quot;&lt;i4&quot;)
c = numpy.column_stack([a, b])
print(a.dtype)  # int32   okay
print(b.dtype)  # int32   okay
print(c.dtype)  # int32   okay
print()

a = numpy.array([1, 2, 3], dtype=&quot;&gt;i4&quot;)
b = numpy.array([1, 2, 3], dtype=&quot;&gt;i4&quot;)
c = numpy.column_stack([a, b])
print(a.dtype)  # &gt;i4     okay
print(b.dtype)  # &gt;i4     okay
print(c.dtype)  # int32   WRONG
print()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span>

<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"i4"</span>)
<span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"i4"</span>)
<span class="pl-s1">c</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">column_stack</span>([<span class="pl-s1">a</span>, <span class="pl-s1">b</span>])
<span class="pl-en">print</span>(<span class="pl-s1">a</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">b</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">c</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>()

<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"=i4"</span>)
<span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"=i4"</span>)
<span class="pl-s1">c</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">column_stack</span>([<span class="pl-s1">a</span>, <span class="pl-s1">b</span>])
<span class="pl-en">print</span>(<span class="pl-s1">a</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">b</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">c</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>()

<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"&lt;i4"</span>)
<span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"&lt;i4"</span>)
<span class="pl-s1">c</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">column_stack</span>([<span class="pl-s1">a</span>, <span class="pl-s1">b</span>])
<span class="pl-en">print</span>(<span class="pl-s1">a</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">b</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">c</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   okay</span>
<span class="pl-en">print</span>()

<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"&gt;i4"</span>)
<span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"&gt;i4"</span>)
<span class="pl-s1">c</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">column_stack</span>([<span class="pl-s1">a</span>, <span class="pl-s1">b</span>])
<span class="pl-en">print</span>(<span class="pl-s1">a</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># &gt;i4     okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">b</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># &gt;i4     okay</span>
<span class="pl-en">print</span>(<span class="pl-s1">c</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32   WRONG</span>
<span class="pl-en">print</span>()</pre></div>
<p dir="auto"><em>Edit</em>:<br>
Even more obvious:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy

a = numpy.array([1, 2, 3], dtype=&quot;&gt;i4&quot;)

print(a.dtype)  # &gt;i4
print((+a).dtype)  # int32 !"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span>

<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">"&gt;i4"</span>)

<span class="pl-en">print</span>(<span class="pl-s1">a</span>.<span class="pl-s1">dtype</span>)  <span class="pl-c"># &gt;i4</span>
<span class="pl-en">print</span>((<span class="pl-c1">+</span><span class="pl-s1">a</span>).<span class="pl-s1">dtype</span>)  <span class="pl-c"># int32 !</span></pre></div>