2

I have a script written where a list is created from the column of a file. Within this list there are many nan entries randomly placed. How can i remove these entries? Heres my code :

#import astropy.io.ascii as asciitable
import numpy as np
import pylab as plt

#x=asciitable.read('protected.txt', guess=False,delimiter='\t',fill_values=[('', '-999')])
#x=np.genfromtxt('protected.txt', comments='#', delimiter='   ', skiprows=0, skip_header=0, skip_footer=0, converters=None, missing='', missing_values='', filling_values=-999, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True)
x=np.load('EC/EC_data')
# Convert columns to float values
BMI=map(float,x['bmiEC'])
print BMI

heres the traceback:

TypeError                                 Traceback (most recent call last)

/example_bmiEC.py in <module>()
      8 # Convert columns to float values

      9 BMI=map(float,x['bmiEC'])
---> 10 BMI=BMI[~np.isnan(BMI)]
     11 print BMI



TypeError: only integer arrays with one element can be converted to an index
WARNING: Failure executing file: <example_bmiECFSPR.py>
blablabla
  • 304
  • 1
  • 8
  • 18
  • Could you clearly explain what a `nan entry` is? / And why is half your code commented out? What exactly are you asking? What have you tried? – Inbar Rose Aug 21 '13 at 14:47
  • possible duplicate of [How to check for NaN in python?](http://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python) – Colin D Aug 21 '13 at 14:51
  • Do you want to remove the NaNs from the resulting list, or do you want to fix it so that they don't appear there in the first place? – JJJ Aug 21 '13 at 14:51
  • nan means 'not a number'. Some of the coding is commented out because it is not needed for the computer im running the code on but on others it is – blablabla Aug 21 '13 at 14:51
  • @Juhana either remove or fix so they dont appear would be good – blablabla Aug 21 '13 at 14:53

2 Answers2

3
>>> import numpy as np
>>> a = np.array([1, 2, 3., np.nan, 4, np.nan])
>>> a = a[~np.isnan(a)]
>>> a
array([ 1.,  2.,  3.,  4.])
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

How to check for NaN in python?

See this answer,

Basically use the function math.isnan()

>>> import math
>>> x=float('nan')
>>> math.isnan(x)
True
>>>
Community
  • 1
  • 1
beiller
  • 3,105
  • 1
  • 11
  • 19