I want to set specific values in a numpy array to NaN
(to exclude them from a row-wise mean calculation).
I tried
import numpy
x = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]])
cutoff = [5, 7]
for i in range(len(x)):
x[i][0:cutoff[i]:1] = numpy.nan
Looking at x
, I only see -9223372036854775808
where I expect NaN
.
I thought about an alternative:
for i in range(len(x)):
for k in range(cutoff[i]):
x[i][k] = numpy.nan
Nothing happens. What am I doing wrong?
Best Answer
nan
is a floating-point value. Whenx
is an array with integer dtype, it can not be assigned a nan value. Whennan
is assigned to an array of integer dtype, the value is automatically converted to an int:So to fix your code, make
x
an array of float dtype:yields