1

I'm currently encounter a TypeError: 'numpy.bool_' object is not iterable without knowing why. I've verified each variables that I used. Do you have any idea why this error occurs? Here a simplified code of mine that you can use to play with :

import numpy as np


def rad_to_deg(x):
    deg = x*180/np.pi
    return deg

def flag_PA(x,bornes_test):  
    index_0_test = int(np.where(bornes_test==0)[0][0])
    for i in range(index_0_test):
        cond1 = any(bornes_test[i] <= x < bornes_test[i+1])
        cond2 = any(bornes_test[i]+180 <= x < bornes_test[i+1]+180)
        if np.logical_or(cond1,cond2)==True :
            flag_test=i 
    return flag_test


##################################### DATA READING ###########################

# Trouver les fichiers en Bande L et Bande N 


bornes_PA = np.linspace(-180,180,18+1)

lambda_fichier       = np.linspace(3E-6,11E-6)
u_coord_fichier      = np.linspace(1,40)
v_coord_fichier      = np.linspace(1,40)
baseline_fichier     = np.sqrt(np.array(u_coord_fichier)**2+np.array(v_coord_fichier)**2).tolist()
for l in range(len(lambda_fichier)):
    for b in range(len(baseline_fichier)):
        deg = rad_to_deg(np.arctan2(u_coord_fichier[b],v_coord_fichier[b]))
        result = int(flag_PA(deg,bornes_PA))

Here is the full error :

  line 34, in <module>
    result = int(flag_PA(deg,bornes_PA))

  line 13, in flag_PA
    cond1 = any(bornes_test[i] <= x < bornes_test[i+1])
1
  • 1
    Before I even look at the question, I can tell you what TypeError: 'numpy.bool_' object is not iterable means. It means that you have tried to iterate over an instance of numpy.bool_, which was likely an element in an array. Now the task is to figure out where and why this happened... Commented Apr 15, 2020 at 15:06

1 Answer 1

3

The problem is that any tries to iterate over its argument.

The expression bornes_test[i] <= x < bornes_test[i+1] returns a scalar numpy.bool_, not an array.

Therefore any(bornes_test[i] <= x < bornes_test[i+1]) is trying to iterate over a scalar, as stated by the error message.

It looks like you can just delete the any and obtain the intended result.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect ! Thank you for this clarification. This was indeed, any which caused the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.