Search code examples
pythonpython-3.xnumpynan

In Python, why did I get -9223372036854775808 when I set one row of a 2D array as np.nan?


In Python, if I define a 2D array, and set the second row as np.nan, the second row will become all -9223372036854775808 rather than missing values. An example is here:

b = np.array(
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
 [0, 3, 4, 4, 6, 6, 6, 5, 4, 5],
 [0, 0, 0, 3, 6, 6, 6, 6, 6, 6],
 [0, 0, 3, 4, 6, 6, 6, 6, 6, 6],
 [0, 1, 2, 4, 4, 4, 4, 4, 4, 4]])

b[1, :] = np.nan
print(b)

[[                   0                    0                    0
                 0                    0                    0
                 0                    0                    0
                 5]
[-9223372036854775808 -9223372036854775808 -9223372036854775808
 -9223372036854775808 -9223372036854775808 -9223372036854775808
 -9223372036854775808 -9223372036854775808 -9223372036854775808
 -9223372036854775808]
[                   0                    0                    0
                 3                    6                    6
                 6                    6                    6
                 6]
[                   0                    0                    3
                 4                    6                    6
                 6                    6                    6
                 6]
[                   0                    1                    2
                 4                    4                    4
                 4                    4                    4
                 4]]

Does anyone have any idea? And how should I correctly assign one row to np.nan?

For your reference, I am running these codes on python 3.7.10 environment created by mamba on Ubuntu 16.04.7 LTS (GNU/Linux 4.15.0-132-generic x86_64).


Solution

  • np.nan is a special floating point value that cannot be used in integer arrays. Since b is an array of integers, the code b[1, :] = np.nan attempts to convert np.nan to an integer, which is an undefined behavior. See this for a discussion of a similar issue.