<p dir="auto">I found an odd discrepancy when creating a custom dtype in numpy. The code below:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
print &quot;VERSION&quot;, np.__version__

def f(data, size):
    return np.frombuffer(data, np.dtype([('d', 'i', size)]))

d1 = np.arange(1)
d2 = np.arange(2)

a1 = f(d1, 1)
a2 = f(d2, 2)
b1 = f(d1, [1])
b2 = f(d2, [2])

print &quot;a1['d'].shape: &quot;,  a1['d'].shape, &quot; b1['d'].shape: &quot;, b1['d'].shape
print &quot;a2['d'].shape: &quot;,  a2['d'].shape, &quot; b2['d'].shape: &quot;, b2['d'].shape"><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-k">print</span> <span class="pl-s">"VERSION"</span>, <span class="pl-s1">np</span>.<span class="pl-s1">__version__</span>

<span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">data</span>, <span class="pl-s1">size</span>):
    <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">frombuffer</span>(<span class="pl-s1">data</span>, <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>([(<span class="pl-s">'d'</span>, <span class="pl-s">'i'</span>, <span class="pl-s1">size</span>)]))

<span class="pl-s1">d1</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">1</span>)
<span class="pl-s1">d2</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">2</span>)

<span class="pl-s1">a1</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>(<span class="pl-s1">d1</span>, <span class="pl-c1">1</span>)
<span class="pl-s1">a2</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>(<span class="pl-s1">d2</span>, <span class="pl-c1">2</span>)
<span class="pl-s1">b1</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>(<span class="pl-s1">d1</span>, [<span class="pl-c1">1</span>])
<span class="pl-s1">b2</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>(<span class="pl-s1">d2</span>, [<span class="pl-c1">2</span>])

<span class="pl-k">print</span> <span class="pl-s">"a1['d'].shape: "</span>,  <span class="pl-s1">a1</span>[<span class="pl-s">'d'</span>].<span class="pl-s1">shape</span>, <span class="pl-s">" b1['d'].shape: "</span>, <span class="pl-s1">b1</span>[<span class="pl-s">'d'</span>].<span class="pl-s1">shape</span>
<span class="pl-k">print</span> <span class="pl-s">"a2['d'].shape: "</span>,  <span class="pl-s1">a2</span>[<span class="pl-s">'d'</span>].<span class="pl-s1">shape</span>, <span class="pl-s">" b2['d'].shape: "</span>, <span class="pl-s1">b2</span>[<span class="pl-s">'d'</span>].<span class="pl-s1">shape</span></pre></div>
<p dir="auto">produces this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="VERSION 1.12.1
a1['d'].shape:  (1L,)  b1['d'].shape:  (1L, 1L)
a2['d'].shape:  (1L, 2L)  b2['d'].shape:  (1L, 2L)"><pre class="notranslate"><code class="notranslate">VERSION 1.12.1
a1['d'].shape:  (1L,)  b1['d'].shape:  (1L, 1L)
a2['d'].shape:  (1L, 2L)  b2['d'].shape:  (1L, 2L)
</code></pre></div>
<p dir="auto">where <code class="notranslate">a1['d'].shape</code> is the odd one out. This inconsistency is annoying... but at least it seems like always defining the size with a list produces consistent behavior.</p>
<p dir="auto">I did a quick search and I didn't see a duplicate, but I might have missed it.</p>
<p dir="auto">So for context this question comes from this post on stackoverflow: <a href="https://stackoverflow.com/questions/50047174/array-comparison-not-matching-elementwise-comparison-in-numpy#50047327" rel="nofollow">https://stackoverflow.com/questions/50047174/array-comparison-not-matching-elementwise-comparison-in-numpy#50047327</a></p>
<blockquote>
<p dir="auto">I have a numpy array arr. It's a numpy.ndarray, size is (5553110,), dtype=float32.</p>
<p dir="auto">When I do:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(arr &gt; np.pi )[3154950]
False
(arr[3154950] &gt; np.pi )
True"><pre class="notranslate"><code class="notranslate">(arr &gt; np.pi )[3154950]
False
(arr[3154950] &gt; np.pi )
True
</code></pre></div>
<p dir="auto">Why is the first comparison getting it wrong? And how can I fix it?</p>
<p dir="auto">The values:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="arr[3154950]= 3.1415927
np.pi= 3.141592653589793"><pre class="notranslate"><code class="notranslate">arr[3154950]= 3.1415927
np.pi= 3.141592653589793
</code></pre></div>
<p dir="auto">Is the problem with precision?</p>
</blockquote>
<p dir="auto">Would someone be able to explain the difference in the way numpy approaches the problem? It looks like using np.float64 explicitly fixes the problem, but I'm curious to know if numpy does a different casting in the two indexing methods.</p>