title: Python: data type conversion of numpy array containing NaN
body: <p>I am incurring in a behaviour that I cannot comprehend.</p>
  <h2>Problem description</h2>
  <p>I was hoping that trying to convert the data type of an array to integer would raise the &quot;classic&quot; <code>ValueError: cannot convert float NaN to integer</code> when the array contains NaN.
  This unfortunately does not happen.</p>
  <p>At a first glance this seems not happening because the NaN contained in a numpy array are converted to <code>numpy.float64</code> instead of &quot;remaining&quot; <code>float</code> as per <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html" rel="nofollow noreferrer"><code>numpy.ndarray</code></a> documentation.</p>
  <p>In fact, consider this example:</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  arr = np.array([np.nan, 1])
  
  print(type(arr[0]))      # Output: &lt;class 'numpy.float64'&gt;
  
  
  # Converting numpy.float64 NaN to other data types
  
  ## numpy integers
  print( np.int8(arr[0]))  # Output: 0 
  print(np.int32(arr[0]))  # Output: 0
  print(np.int32(arr[0]))  # Output:          -2147483648 (i.e. -2 ** 31)
  print(np.int64(arr[0]))  # Output: -9223372036854775808 (i.e. -2 ** 63)
  
  ## numpy unsigned integers
  print( np.uint8(arr[0]))  # Output: 0
  print(np.uint16(arr[0]))  # Output: 0
  print(np.uint32(arr[0]))  # Output: 0
  print(np.uint64(arr[0]))  # Output: 9223372036854775808
  
  ## numpy floats
  print(np.float16(arr[0]))  # Output: NaN
  print(np.float32(arr[0]))  # Output: NaN
  print(np.float64(arr[0]))  # Output: NaN
  
  
  # Converting numpy.float NaN to other data types
  
  ## np.float and np.int are aliases to float and int respectively (both np.float and np.int are deprecated)
  type(np.nan) == type(np.float(np.nan))  # Output: True
  print(np.int(np.nan))                   # Output: ValueError: cannot convert float NaN to integer
  
  ## numpy integers
  print( np.int8(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  print(np.int16(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  print(np.int32(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  print(np.int64(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  
  ## numpy unsigned integers
  print( np.uint8(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  print(np.uint16(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  print(np.uint32(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  print(np.uint64(np.nan))  # Output: ValueError: cannot convert float NaN to integer
  
  ## numpy floats
  print(np.float16(np.nan))  # Output: NaN
  print(np.float32(np.nan))  # Output: NaN
  print(np.float64(np.nan))  # Output: NaN
  </code></pre>
  <h2>Questions</h2>
  <ul>
  <li>Why the elements of <code>arr</code> are converted to <code>numpy.float64</code> instead of <code>float</code> in the first place?</li>
  <li>In what exactly do python built-in <code>float</code> and <code>numpy.float64</code> differ?</li>
  <li>Why <code>np.int32(arr)</code> and <code>np.int64(arr)</code> contain the &quot;smallest possible int&quot; while <code>np.int8(arr)</code> and <code>np.int16(arr)</code> contain zeros? (Similar question for unsigned integer types)</li>
  </ul>
  <h2>Edit</h2>
  <p>I am experiencing this behaviour on multiple platform/python/numpy versions, see the three examples below.</p>
  <pre class="lang-py prettyprint-override"><code>import platform
  import sys
  import numpy
  print('Platform:', platform.platform())
  print('Python version:', sys.version)
  print('numpy.__version__:', numpy.__version__)
  </code></pre>
  <h3>WSL</h3>
  <pre class="lang-py prettyprint-override"><code>Platform: Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.31
  Python version: 3.9.7 (default, Mar  3 2022, 13:49:04) 
  [GCC 9.3.0]
  numpy.__version__: 1.21.5
  </code></pre>
  <h3>Windows</h3>
  <pre class="lang-py prettyprint-override"><code>Platform: Windows-10-10.0.22593-SP0
  Python version: 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
  numpy.__version__: 1.21.0
  </code></pre>
  <h3>Online <a href="https://www.w3schools.com/python/trypython.asp?filename=demo_default" rel="nofollow noreferrer">W3Schools</a></h3>
  <pre class="lang-py prettyprint-override"><code>Platform: Linux-4.19.0-18-amd64-x86_64-with-glibc2.29
  Python version: 3.8.2 (default, Mar 13 2020, 10:14:16) 
  [GCC 9.3.0]
  numpy.__version__: 1.18.2
  </code></pre>