<p dir="auto">A bug or perhaps a surprise: in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="93831872" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/6053" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/6053/hovercard" href="https://github.com/numpy/numpy/pull/6053">#6053</a>, assignment for structured arrays was changed to be by position rather than field name. However, it seems equality checking was forgotten, leading to the following strange behaviour, where one assigns something but cannot then test equality. It would seem an oversight, so cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ahaldane/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ahaldane">@ahaldane</a>, who did <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="93831872" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/6053" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/6053/hovercard" href="https://github.com/numpy/numpy/pull/6053">#6053</a>.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.21.0.dev0+215.gd7a75e8e8'

In [3]: a = np.array([(1, 2), (3, 4)], dtype=[('a', 'i'), ('b', 'i')])

In [4]: b = np.zeros(2, dtype='i,i')

In [5]: a
Out[5]: array([(1, 2), (3, 4)], dtype=[('a', '&lt;i4'), ('b', '&lt;i4')])

In [6]: b
Out[6]: array([(0, 0), (0, 0)], dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])

In [7]: b[:] = a

In [8]: b
Out[8]: array([(1, 2), (3, 4)], dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])

In [9]: a == b
&lt;ipython-input-9-eeadcaeab874&gt;:1: FutureWarning: elementwise == comparison failed and returning scalar instead; this will raise an error or perform elementwise comparison in the future.
  a == b
Out[9]: False

In [10]: a == b.astype(a.dtype)
Out[10]: array([ True,  True])"><pre class="notranslate"><code class="notranslate">In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.21.0.dev0+215.gd7a75e8e8'

In [3]: a = np.array([(1, 2), (3, 4)], dtype=[('a', 'i'), ('b', 'i')])

In [4]: b = np.zeros(2, dtype='i,i')

In [5]: a
Out[5]: array([(1, 2), (3, 4)], dtype=[('a', '&lt;i4'), ('b', '&lt;i4')])

In [6]: b
Out[6]: array([(0, 0), (0, 0)], dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])

In [7]: b[:] = a

In [8]: b
Out[8]: array([(1, 2), (3, 4)], dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])

In [9]: a == b
&lt;ipython-input-9-eeadcaeab874&gt;:1: FutureWarning: elementwise == comparison failed and returning scalar instead; this will raise an error or perform elementwise comparison in the future.
  a == b
Out[9]: False

In [10]: a == b.astype(a.dtype)
Out[10]: array([ True,  True])
</code></pre></div>
<p dir="auto">p.s. <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/seberg/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/seberg">@seberg</a> - is it expected that the cast cannot be done as "safe"? Or is this still in the process of being changed?</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [12]: a == b.astype(a.dtype, casting='safe')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-12-896f15d3d149&gt; in &lt;module&gt;
----&gt; 1 a == b.astype(a.dtype, casting='safe')

TypeError: Cannot cast array data from dtype([('f0', '&lt;i4'), ('f1', '&lt;i4')]) to dtype([('a', '&lt;i4'), ('b', '&lt;i4')]) according to the rule 'safe'"><pre class="notranslate"><code class="notranslate">In [12]: a == b.astype(a.dtype, casting='safe')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-12-896f15d3d149&gt; in &lt;module&gt;
----&gt; 1 a == b.astype(a.dtype, casting='safe')

TypeError: Cannot cast array data from dtype([('f0', '&lt;i4'), ('f1', '&lt;i4')]) to dtype([('a', '&lt;i4'), ('b', '&lt;i4')]) according to the rule 'safe'
</code></pre></div>
<h3 dir="auto">Describe the issue:</h3>
<p dir="auto">The range of np.int8 is -128 to 127.<br>
The ideal range of np.abs(np.int8) is -127 to 128.<br>
But apparently 128 overflow.<br>
So np.abs(-128, dtype=int8) = -128, while the rest of the cases are all correct.<br>
This error is hard to detect and is also found in np.int16.</p>
<h3 dir="auto">Reproduce the code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
a = np.array([-128, -127], dtype=np.int8)
print(a)
# [-128 -127]

print(np.abs(a))
# [-128, 127]"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">-</span><span class="pl-c1">128</span>, <span class="pl-c1">-</span><span class="pl-c1">127</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">int8</span>)
<span class="pl-en">print</span>(<span class="pl-s1">a</span>)
<span class="pl-c"># [-128 -127]</span>

<span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-en">abs</span>(<span class="pl-s1">a</span>))
<span class="pl-c"># [-128, 127]</span></pre></div>
<h3 dir="auto">Error message:</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">NumPy/Python version information:</h3>
<p dir="auto">numpy: 1.18.5<br>
sys: 3.7.10 (default, Jun  4 2021, 14:48:32)<br>
[GCC 7.5.0]</p>