Skip to content
Advertisement

TypeError: ‘<' not supported between instances of 'numpy.ndarray' and 'int' with a csv file

I am trying to find specific elements (those with a value less than 5) in a particular data column from an imported csv file. However I keep encountering this strange error. The code reads as follows:

z = np.loadtxt('exoplanets_nasa_archive.csv', delimiter= ',', dtype=str )

zlabels = z[0]

#print(zlabels)

mass =z[1:, 9]

subfive =np.where(mass<5)```

#The output error reads:

TypeError                                 Traceback (most recent call last)
<ipython-input-202-af2622a146c5> in <module>()
----> 1 subfive =np.where(mass<5)

TypeError: '<' not supported between instances of 'numpy.ndarray' and 'int'

Advertisement

Answer

Apparently you are doing:

In [78]: mass = np.array([1.23, 123.3], dtype='U32')
In [79]: mass
Out[79]: array(['1.23', '123.3'], dtype='<U32')
In [80]: mass<5
Traceback (most recent call last):
  File "<ipython-input-80-39e4c1351b25>", line 1, in <module>
    mass<5
TypeError: '<' not supported between instances of 'numpy.ndarray' and 'int'

I’m a little surprised that it isn’t complaining about instances of string (or string dtype) and int.

With loadtxt the default dtype is float, and it raises an error if some strings can’t be parsed as floats. np.genfromtxt assigns those values nan instead.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement