beliz
beliz

Reputation: 422

'<=' not supported between instances of 'numpy.str_' and 'float'

I have two arrays, first column contains the element name and the last three columns contain the coordinates. I want to read each line and if the coordinates are in the box I defined, I want to save the whole line including the element name. If I don't include the element name, this code works perfectly. Here is a MWE:

import numpy as np
array1 = np.array([['O', '0.919775', '3.7032729999999994', '6.436484'],
       ['O', '3.295445', '0.22673200000000016', '8.697384'],
       ['C', '5.553644', '2.607080999999999', '4.798966999999999']],
      dtype='<U32')
array2 = np.array([['C', '7.748108', '0.8001009999999997', '4.791696999999999'],
       ['O', '12.701898', '3.257845999999999', '0.6352019999999996'],
       ['H', '13.696655', '3.614818999999999', '-0.1844650000000012'],
       ['H', '12.209518', '4.530944999999999', '1.5239319999999985']],
      dtype='<U32')

cell_a = 6.613
cell_b = 8.358
cell_c = 9.617

list_array = (array1,array2)
for j in list_array:
    for i in range(len(array1)):
        if j[i][1] <= cell_a and j[i][1] >= 0:
            if j[i][2] <= cell_b and j[i][2] >= 0:
                if j[i][3] <= cell_c and j[i][3] >= 0:
                    print(j[i])

And I get the following error:

'<=' not supported between instances of 'numpy.str_' and 'float'

I don't understand, j[i][0] contains the string and I am not working with it...

PS: The code can give no output as I chose the arrays randomly.

Upvotes: 0

Views: 100

Answers (2)

beliz
beliz

Reputation: 422

Okay, I fixed the problem. Changing the dtype of the coordinates from str to float in the loop solves the problem. I made the following changes:

for j in list_array:
    for i in range(len(array1)):
        if float(j[i][1]) <= cell_a and float(j[i][1]) >= 0:
            if float(j[i][2]) <= cell_b and float(j[i][2]) >= 0:
                if float(j[i][3]) <= cell_c and float(j[i][3]) >= 0:
                    print(j[i])

Upvotes: 0

hpaulj
hpaulj

Reputation: 231550

What's so puzzling. The two arrays, as shown, contain strings:

In [318]: array1[0]                                                                                    
Out[318]: array(['O', '0.919775', '3.7032729999999994', '6.436484'], dtype='<U32')
In [319]: array1[0][1]                                                                                 
Out[319]: '0.919775'
In [320]: array1[0][1]<cell_a                                                                          
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-320-73400e758be3> in <module>
----> 1 array1[0][1]<cell_a

TypeError: '<' not supported between instances of 'numpy.str_' and 'float'

Python cannot compare a string with a number!

By the way, in numpy we prefer to access elements with:

In [321]: array1[0, 1]                                                                                 
Out[321]: '0.919775'

here the use [0][1] works too, but the [0,1] syntax is clearer and more reliable.

When you make an array from a list that contains strings, the whole array has string dtype.

The array without the string column can be made floats:

In [322]: array1[:,1:]                                                                                 
Out[322]: 
array([['0.919775', '3.7032729999999994', '6.436484'],
       ['3.295445', '0.22673200000000016', '8.697384'],
       ['5.553644', '2.607080999999999', '4.798966999999999']],
      dtype='<U32')
In [323]: arr = array1[:,1:].astype(float)                                                             
In [324]: arr                                                                                          
Out[324]: 
array([[0.919775, 3.703273, 6.436484],
       [3.295445, 0.226732, 8.697384],
       [5.553644, 2.607081, 4.798967]])
In [325]: arr[0,0]<=cell_a                                                                             
Out[325]: True

Upvotes: 1

Related Questions