0

I want to calculate a text field call "TIME_DAY," depending of whether or not the time falls within of four time periods it should return one of four values. My script should work, but it doesn't.

enter image description here

enter image description here

def timeday(time, am, diur, pm, noc):
    if(time >=am AND time < diur):
        return “cerp_am”
    elif(time>= diur AND time<pm):
        return “diurnal”
    elif(time>=pm AND time<noc):
        return “cerp_pm”
    else:
        return “nocturnal”

 timeday(!LMT_TIME!, !B_C_AM1!, !B_DIUR1!, !B_C_PM1!, !BEG_NOC1!)

I'm getting ERROR 000989: Python syntax error: Parsing error Syntax.

Here is what my attribute table looks like:enter image description here

@Midavalo's solution worked well for the vast majority of my data, except for a few datasets, where it mistakenly places midnight values in the "diurnal" category. enter image description here

  • 1
    What are the values in the fields you are looking up? – Midavalo May 8 '17 at 22:41
  • Times: so if time in LMT_TIME falls between the time in B_C_AM1 and B_DIUR, it should return "cerp_am". – J.W. Powell May 8 '17 at 22:43
  • Midavalo, please see the edited question. – J.W. Powell May 8 '17 at 22:49
3

Without seeing actual values for your fields it's hard to answer, but for a syntax error I imagine it may be the uppercase AND. Change this to and

def timeday(time, am, diur, pm, noc):
    if(time >=am and time < diur):
        return "cerp_am"
    elif(time>= diur and time<pm):
        return "diurnal"
    elif(time>=pm and time<noc):
        return "cerp_pm"
    else:
        return "nocturnal"

Python is case sensitive, so most operators in python should be in lower case only.

Additionally, you will need to fix the quote marks - and should be ". These odd quote marks happen when you copy/paste into and out of something like Microsoft Word as it likes to be helpful and automatically change characters to others that it thinks look nice, but don't flow well into syntax/format dependant code like Python.

  • I'm still getting ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax – J.W. Powell May 8 '17 at 22:51
  • @J.W.Powell Fix the quote marks and should be " – Midavalo May 8 '17 at 22:54
  • 1
    @J.W.Powell assuming that you're writing your code in WordPad or similar, you could try using Notepad++ to avoid the dreaded smart quotes being inserted. – Stephen Lead May 8 '17 at 23:20
  • @Midavalo, this script (or some variation of it) worked perfectly to edit 95% of my data. The only exception is one folder of data, which for some reason, it misidentifies all the times near midnight. The script runs fine, but if the 'TIME' value begins with a zero, it identifies it as "diurnal." Do you have any guesses as to why this might be? – J.W. Powell May 12 '17 at 19:15
  • I was able to fix it, but I had to create a new 'TIME' field. To create the new time field, I used the following script: !Existing_TIME_FIELD!.zfill(8) – J.W. Powell May 12 '17 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.