<p dir="auto">Casting np.arrays to int32 converts <code class="notranslate">np.nan</code> to:</p>
<ol dir="auto">
<li><code class="notranslate">array(-2147483648, dtype=int32)</code> on a local Mac M1 machine</li>
<li><code class="notranslate">array(0, dtype=int32)</code> on a docker image running ubuntu</li>
</ol>
<h3 dir="auto">Reproducing code example:</h3>
<p dir="auto">On the following system:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ProductName:	macOS
ProductVersion:	11.2.3
BuildVersion:	20D91"><pre class="notranslate"><code class="notranslate">ProductName:	macOS
ProductVersion:	11.2.3
BuildVersion:	20D91
</code></pre></div>
<p dir="auto">The following code casts np.nan to -2147483648</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; np.array(np.nan).astype(np.int32)
array(-2147483648, dtype=int32)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <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-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">int32</span>)
<span class="pl-en">array</span>(<span class="pl-c1">-</span><span class="pl-c1">2147483648</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int32</span>)</pre></div>
<p dir="auto">On a docker image with the following system:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# uname -srv
Linux 5.10.25-linuxkit #1 SMP PREEMPT Tue Mar 23 09:24:45 UTC 2021"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#</span> uname -srv</span>
Linux 5.10.25-linuxkit <span class="pl-c"><span class="pl-c">#</span>1 SMP PREEMPT Tue Mar 23 09:24:45 UTC 2021</span></pre></div>
<p dir="auto">The following code casts np.nan to 0</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt;np.array(np.nan).astype(np.int32)
array(0, dtype=int32)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <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-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span><span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">int32</span>)
<span class="pl-en">array</span>(<span class="pl-c1">0</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int32</span>)</pre></div>
<p dir="auto"><strong>Note:</strong> One can run <code class="notranslate">docker run -it python:3 bash</code> to replicate the environment, and <code class="notranslate">pip install numpy</code> which installs from <code class="notranslate">numpy-1.21.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch6</code></p>
<h3 dir="auto">NumPy/Python version information:</h3>
<p dir="auto">In our Mac M1 local machinge</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; sys.version
'3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 15:59:12) \n[Clang 11.0.1 ]'
&gt;&gt;&gt; np.__version__
'1.21.2'"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">sys</span>.<span class="pl-s1">version</span>
<span class="pl-s">'3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 15:59:12) <span class="pl-cce">\n</span>[Clang 11.0.1 ]'</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-s1">__version__</span>
<span class="pl-s">'1.21.2'</span></pre></div>
<p dir="auto">In our Ubuntu image:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; sys.version
'3.7.11 (default, Jul 27 2021, 15:14:22) \n[GCC 10.2.0]'
&gt;&gt;&gt; np.__version__
'1.21.2'"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">sys</span>.<span class="pl-s1">version</span>
<span class="pl-s">'3.7.11 (default, Jul 27 2021, 15:14:22) <span class="pl-cce">\n</span>[GCC 10.2.0]'</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-s1">__version__</span>
<span class="pl-s">'1.21.2'</span></pre></div>
<p dir="auto">With <code class="notranslate">ndarray.astype(..., casting="unsafe")</code> float arrays with NaN/inf will be converted to <code class="notranslate">np.iinfo(dtype).min/max</code>,</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.array([1, np.nan], dtype=np.float32).astype(np.int32)
array([          1, -2147483648], dtype=int32)
&gt;&gt;&gt; np.array([1, np.inf], dtype=np.float32).astype(np.int32)
array([          1, -2147483648], dtype=int32)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">float32</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">int32</span>)
<span class="pl-en">array</span>([          <span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">2147483648</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int32</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-s1">np</span>.<span class="pl-s1">inf</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">float32</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">int32</span>)
<span class="pl-en">array</span>([          <span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">2147483648</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int32</span>)</pre></div>
<p dir="auto">(there are also some inconsistencies there cf <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="97160346" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/6109" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/6109/hovercard" href="https://github.com/numpy/numpy/issues/6109">#6109</a>). This is very bad in practical applications as output data will be wrong by orders of magnitude. Other <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html" rel="nofollow"><code class="notranslate">casting</code> options</a> simply disallow casting float to int.</p>
<p dir="auto">At the same time, casting from <code class="notranslate">dtype=np.object</code> works as expected,</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.array([1, np.inf], dtype=np.object).astype(np.int32)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
OverflowError: cannot convert float infinity to integer"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-s1">np</span>.<span class="pl-s1">inf</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">object</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">int32</span>)
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
<span class="pl-v">OverflowError</span>: <span class="pl-s1">cannot</span> <span class="pl-s1">convert</span> <span class="pl-s1">float</span> <span class="pl-s1">infinity</span> <span class="pl-s1">to</span> <span class="pl-s1">integer</span></pre></div>
<p dir="auto">It could be useful to have some additional <code class="notranslate">casting</code> option allowing to convert int to float, but would error on NaN or inf. This could be done manually,</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="if array.dtype.kind == 'f' and np.dtype(dtype).kind == 'i':
   # check for overflows and NaN
   _assert_all_finite(array)
result = array.astype(dtype)"><pre class="notranslate"><span class="pl-k">if</span> <span class="pl-s1">array</span>.<span class="pl-s1">dtype</span>.<span class="pl-s1">kind</span> <span class="pl-c1">==</span> <span class="pl-s">'f'</span> <span class="pl-c1">and</span> <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>(<span class="pl-s1">dtype</span>).<span class="pl-s1">kind</span> <span class="pl-c1">==</span> <span class="pl-s">'i'</span>:
   <span class="pl-c"># check for overflows and NaN</span>
   <span class="pl-en">_assert_all_finite</span>(<span class="pl-s1">array</span>)
<span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-s1">array</span>.<span class="pl-en">astype</span>(<span class="pl-s1">dtype</span>)</pre></div>
<p dir="auto">but having it in numpy would be useful.</p>
<p dir="auto">I also wonder if there is really a case when not raising on such conversions is meaningful (even with <code class="notranslate">casting='unsafe'</code>). For instance pandas does this conversions as expected (using numpy dtypes),</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; pd.Series([1, np.nan], dtype=np.float64).astype(np.int)
[...]
ValueError: Cannot convert non-finite values (NA or inf) to integer"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>([<span class="pl-c1">1</span>, <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">float64</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">int</span>)
[...]
<span class="pl-v">ValueError</span>: <span class="pl-v">Cannot</span> <span class="pl-s1">convert</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">finite</span> <span class="pl-en">values</span> (<span class="pl-v">NA</span> <span class="pl-c1">or</span> <span class="pl-s1">inf</span>) <span class="pl-s1">to</span> <span class="pl-s1">integer</span></pre></div>
<h3 dir="auto">Numpy/Python version information:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import sys, numpy; print(numpy.__version__, sys.version)
1.16.4 3.7.3 (default, Mar 27 2019, 22:11:17) [GCC 7.3.0]"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; import sys, numpy; print(numpy.__version__, sys.version)
1.16.4 3.7.3 (default, Mar 27 2019, 22:11:17) [GCC 7.3.0]
</code></pre></div>