Valueerror: Attempt to Get Argmax of an Empty Sequence

I get the following error when running the code below.

ValueError: attempt to get argmax of an empty sequence

The code is processing information from images sent to it from simulator.

It runs well at first but when the array Rover.nav_angles is empty i get the error although there is an if condition

if Rover.nav_angles is not None:
        Max_angle_points=np.argmax(Rover.nav_angles)
        MAX_DIST=np.max(Rover.nav_dists[Max_angle_points])
4

2 Answers

Use:

if Rover.nav_angles:
    ...

To check for emptiness and None. But it seems that you deal with numpy array so use:

if Rover.nav_angles.size:
    ...
2

In python it is mostly conceived better to try things rather than setting conditions around it.

try: 
    Max_angle_points=np.argmax(Rover.nav_angles)
    MAX_DIST=np.max(Rover.nav_dists[Max_angle_points])
except ValueError:
    pass
   # specify what the code should do, if the exception occurs.

To your actual problem: empty does not necessarily mean that your array is None. If you want to check with a condition, try

if Rover.nav_angles:
    Max_angle_points=np.argmax(Rover.nav_angles)
    MAX_DIST=np.max(Rover.nav_dists[Max_angle_points])

Your Answer

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

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest