user531525
user531525

Reputation: 87

Recode missing data Numpy

I am reading in census data using the matplotlib cvs2rec function - works fine gives me a nice ndarray.

But there are several columns where all the values are '"none"" with dtype |04. This is cuasing problems when I lode into Atpy "TypeError: object of NoneType has no len()". Something like '9999' or other missing would work for me. Mask is not going to work in this case because I am passing the real array to ATPY and it will not convert MASK. The Put function in numpy will not work with none values wich is the best way to change values(I think). I think some sort of boolean array is the way to go but I can't get it to work.

So what is a good/fast way to change none values and/or uninitialized numpy array to something like '9999'or other recode. No Masking.

Thanks,

Matthew

Upvotes: 2

Views: 2252

Answers (2)

HYRY
HYRY

Reputation: 97331

you can use mask array when you do calculation. and when pass the array to ATPY, you can call filled(9999) method of the mask array to convert the mask array to normal array with invalid values replaced by 9999.

Upvotes: 0

DaveP
DaveP

Reputation: 7102

Here is a solution to this problem, although if your data is a record array you should only apply this operation to your column, rather than the whole array:

import numpy as np
# initialise some data with None in it
a = np.array([1, 2, 3, None])
a = np.where(a == np.array(None), 9999, a)

Note that you need to cast None into a numpy array for this to work

Upvotes: 3

Related Questions