Numpy integer nan
No, you can't, at least with current version of NumPy. A nan
is a special value for float arrays only.
There are talks about introducing a special bit that would allow non-float arrays to store what in practice would correspond to a nan
, but so far (2012/10), it's only talks.
In the meantime, you may want to consider the numpy.ma
package: instead of picking an invalid integer like -99999, you could use the special numpy.ma.masked
value to represent an invalid value.
a = np.ma.array([1,2,3,4,5], dtype=int)
a[1] = np.ma.masked
masked_array(data = [1 -- 3 4 5],
mask = [False True False False False],
fill_value = 999999)
Numpy : check for integer NaN
np.nan
is float and not an integer. You either have to change your dtype for last column or use a different structure to store your nan as integer.
datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<f4') ]
arr = np.full((4,), np.nan, dtype=datadef)
# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'
Now try printing np.isnan statement:
print(np.isnan(arr[1][3]))
True
NumPy or Pandas: Keeping array type as integer while having a NaN value
This capability has been added to pandas beginning with version 0.24.
At this point, it requires the use of extension dtype 'Int64'
(capitalized), rather than the default dtype 'int64'
(lowercase).
Why is numpy telling me that I can't convert a float NaN when I'm actually trying to convert an int?
In numpy
, nan
is a special float
:
>>> type(np.nan)
<class 'float'>
I call it a special type of float because normally Python won't complain if you try to add both int
and float
into a single numpy array since the integers will automatically be converted to float
:
>>> x = np.array([-1, float(10.12)])
>>> x
>> array([-1. , 10.12])
but this is not the case for numpy.nan
:
IEEE 754 floating point representation of Not a Number (NaN).
I would suggest to either create a numpy array of floats:
import numpy as np
x = np.array([15.0, 22.0, 3.0])
x[1] = np.nan
>>> array([15., nan, 3.])
or try to represent nan
using an alternative integer value if your use-case allows this representation. For instance use -1
to represent nan
(for example this would make sense if in your use case you don't have negative values).
Alternatively, you can use a list
that allows you to store objects of different data type:
l = [15, 22, 3]
l[1] = np.nan
>>> [15, nan, 3]
Numpy: ValueError: cannot convert float NaN to integer (Python)
Designate a type for your array of float32
(or float16
, float64
, etc. as appropriate)
import numpy as np
A = np.array([10, 20, 30, 40, 50, 60, 70], dtype=np.float32)
C=[2,4]
A=np.insert(A,C,np.NaN,axis=0)
print("A =",[A])
A = [array([10., 20., nan, 30., 40., nan, 50., 60., 70.], dtype=float32)]
Replace the zeros in a NumPy integer array with nan
np.nan
has type float
: arrays containing it must also have this datatype (or the complex
or object
datatype) so you may need to cast arr
before you try to assign this value.
The error arises because the string value 'nan'
can't be converted to an integer type to match arr
's type.
>>> arr = arr.astype('float')
>>> arr[arr == 0] = 'nan' # or use np.nan
>>> arr
array([[ nan, 1., 2.],
[ 3., 4., 5.]])
Why is np nan convertible to int by `astype` (but not by `int`)?
.astype
has optional argument casting
whose default value is 'unsafe'
. Following values are allowed
- ‘no’ means the data types should not be cast at all.
- ‘equiv’ means only byte-order changes are allowed.
- ‘safe’ means only casts which can preserve values are allowed.
- ‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.
- ‘unsafe’ means any data conversions may be done.
When one attempt to do
import numpy as np
print(np.array([np.nan]).astype(int, casting="safe"))
one gets following error
TypeError: Cannot cast array from dtype('float64') to dtype('int32') according to the rule 'safe'
Related Topics
How to Extract Data from Matplotlib Plot
Python Dictionary:Typeerror: Unhashable Type: 'List'
How to Automatically Fix an Invalid JSON String
How to Make Python Scripts Executable on Windows
How to Use SQL Parameters with Python
Removing Duplicates from Dictionary
Python Ctypes Issue on Different Oses
Imports in _Init_.Py and 'Import As' Statement
Modules Are Installed Using Pip on Osx But Not Found When Importing
Functions That Help to Understand JSON(Dict) Structure
Which Is Faster in Python: X**.5 or Math.Sqrt(X)
How Does the Key Argument in Python's Sorted Function Work
How to Reverse Lists in Python, Getting "Nonetype" as List
Pandas: Cast Column to String Does Not Work
Verifying Compatibility in Compiling Extension Types, and Using Them with Cdef
How to Implement Option Buttons and Change the Button Color in Pygame
Cannot Return Results from Stored Procedure Using Python Cursor