-3

I am trying to set up a function in a way so that it will only execute if two conditions are met: if the variable is greater than some value and smaller than a another value.

I have two functions over this one that define start_time and end_time, as well as a loop that processes the files. As you can see by my if statement, I am trying to read a file's data within a range of numbers. When I set it as I did, however, I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I don't understand how to solve this, especially because I am using two variables (start_date, end_date: both are given a numeric value on the previous function).

In short, how to I make my desired "if" statement possible?

Edit: In addition, I want the files that don't meet the criteria to be ignored, and I am not sure if they will be if I don't write an "else" statement.

xyzman
  • 11
  • 7
  • 2
    Why don't you do what the error message says? – Marcin Jul 29 '16 at 17:04
  • 1
    Your code is really hard to read. Are you allergic to whitespace? Meaningful variable names? Explanatory comments? – Lightness Races in Orbit Jul 29 '16 at 17:07
  • because there don't seem to be any explanations on how to use it that match my case. Thanks for helping, though, I really appreciate it @Marcin – xyzman Jul 29 '16 at 17:09
  • `if np.all( (start_date < juld) && (juld < end_date) ):` –  Jul 29 '16 at 17:09
  • Why are all those hash marks around your function name? I don't even... – kylieCatt Jul 29 '16 at 17:10
  • But you probably want to set a mask: `mask = (start_date < juld) && (juld < end_date)`, and use that to index the remaining part of the code. No if needed. –  Jul 29 '16 at 17:10
  • Because it's a program that has 8 functions, thanks for your input. @IanAuld – xyzman Jul 29 '16 at 17:15
  • @Evert , the first solution resulted in the same error. How should I incorporate the 'mask' into the 'if' statement then? – xyzman Jul 29 '16 at 17:16
  • I don't see what having eight functions has to do with vandalising your code ^_^ – Lightness Races in Orbit Jul 29 '16 at 17:17
  • That is not good coding style. Eight functions isn't a lot but if you find the file hard to navigate don't clutter it up excess character move your functions to separate files and place them in a package. Read PEP-8 [here](https://www.python.org/dev/peps/pep-0008/) – kylieCatt Jul 29 '16 at 17:18
  • There was an error, but it shouldn't be the same error. Double check your parentheses, and change `&&` to `&`: `if np.all( (start_date < juld) & (juld < end_date) ):` –  Jul 29 '16 at 17:19
  • Possible duplicate of [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](http://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Łukasz Rogalski Jul 29 '16 at 17:28
  • The resulted in "TypeError: 'NoneType' object is not iterable". In my main program, I have a part saying 'temp, lon, lat, juld=Read_Data(fl,start_date,end_date)' and the error is referring to it @Evert – xyzman Jul 29 '16 at 17:28
  • Because if the if-clause is False, you automatically return `None` from `Read_Data`. That's what the error is telling you. –  Jul 30 '16 at 01:45

4 Answers4

1

The error is from the fact you are comparing an array (a.k.a. juld) to a number. In short, you need to specify either a specific element for the if statement to check, or use the any() or all() methods as detailed in the error message. You can find the definition of these methods here.

Anthon
  • 69,918
  • 32
  • 186
  • 246
BrandonM
  • 390
  • 4
  • 12
1

First of all, I think you'd wanna look at the min/max value of juld, as it is an array. Try something like:

if max(juld) < start_date or min(juld) > end_date:

I think that should work!

jstack
  • 33
  • 1
  • 8
0

I don't really understand what you want to do here, but there are three options. If you only want to go into the block if all values in the array satisfy it, use a.all(condition). If you want to go in if any values satisfy the condition, use a.any(condition). If you want to go in to the block for each value of the array that satisfies the value, you would do

for x in array:
    if(condition):
        do stuff
    else:
        do other stuff
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

The problem is because you are comparing an array i.e. juld to two dates/numbers, start_date and end_date. So you need to be able to change the array to a date/number. I can't see the what is in juld but I suspect if you just change your code to:

if start_date<juld[0]<end_date:

then it will work i.e. the following works (returns 1):

import datetime
d1 = datetime.date(1996, 4, 1)
d2 = datetime.date(2017, 7, 29)
dt = datetime.date(2016, 7, 29)
x = Read_Data(dt, d1 , d2)

where:

def Read_Data(date, start_date, end_date):
    if start_date<date<end_date:
        return 1
  • so when you write juld[0], you are setting the threshhold to be 0? My values are in the 20,000's as I am using Julian Dates, so could you elaborate on that a little? Thanks – xyzman Jul 29 '16 at 17:37
  • Let me check this `np.array(ncfile.variables['JULD'][:])` and revert. – cpemberton Jul 29 '16 at 17:47
  • Do you expect only one date to come from this call?: `np.array(ncfile.variables['JULD'][:])` If so then print it out like `print(juld)` If the date you are looking for is the 0th element in that array then the solution I wrote above is correct. If, however, you expect more than one date and you want to perform operations on these dates then I would suggest that you iterate through the array like `for each d in juld`. I hope this helps! Sorry you are having a hard time with this! – cpemberton Jul 29 '16 at 18:20
  • Didn't quite solve the problem, but it got me to a better place. Thanks for taking your time and helping me solve this, now I just need to figure out where to place what you gave me. – xyzman Jul 29 '16 at 18:29