STE_V_P
STE_V_P

Reputation: 41

Python Numpy nan_to_num help

I'm having some trouble with Numpy's nan_to_num function: I've got an array where the last column contains fewer elements than the other columns, and when I import it into Python, it places 'nan's to fill out the array. That's just fine until I need to do some other things that get tripped-up by the 'nan's.

I'm trying to use 'nan_to_num' with no success. It's likely a small thing I'm missing, but I can't figure it out.

Here's some simple input and output:

input:

a = numpy.array([[1, nan, 3]])
print a
numpy.nan_to_num(a)
print a

output

[[  1.  nan   3.]]
[[  1.  nan   3.]]

The second 'nan' should be a zero... http://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html

Thanks in advance.

Upvotes: 4

Views: 2634

Answers (1)

Josh
Josh

Reputation: 2196

You haven't changed the value of a. Try this:

a = numpy.array([[1, nan, 3]])
a = numpy.nan_to_num(a)
print a

Upvotes: 4

Related Questions