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>
5
  • 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
    Commented Aug 21, 2013 at 14:47
  • possible duplicate of How to check for NaN in python?
    – Colin D
    Commented Aug 21, 2013 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
    Commented Aug 21, 2013 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
    Commented Aug 21, 2013 at 14:51
  • @Juhana either remove or fix so they dont appear would be good
    – blablabla
    Commented Aug 21, 2013 at 14:53

2 Answers 2

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.])
6
  • 1
    symilarly to the ~ operator you can use a[np.logical_not(np.isnan(a))] Commented Aug 21, 2013 at 15:12
  • @SaulloCastro neither of those work for me, i get an error saying 'only integer arrays with one element can be converted to an index'
    – blablabla
    Commented Aug 21, 2013 at 15:17
  • @blablabla I am afraid the error is somewhere else... could you please update the question with the taceback of your error? Commented Aug 21, 2013 at 15:28
  • 1
    @blablabla: You need to convert BMI to a NumPy array: BMI = np.array(BMI)
    – NPE
    Commented Aug 21, 2013 at 15:48
  • 1
    @blablabla... in Python 2.x the output of map is a list object, so @NPE is right, you must convert it to a numpy.ndarray Commented Aug 21, 2013 at 16:05
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
>>>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.