1

Using ArcMap - I would like to calculate one field based on the value of another field. I want to Update the field 'Date_Class' based on the value in another field 'MIN_YEAR_B'. I was trying to do this with Python, using:

Code Block:

def ReClass (Date_Class):
 if (MIN_YEAR_B >= 1600 and MIN_YEAR_B <= 1650):
  Date_Class = 1
 elif (MIN_YEAR_B > 1650 and MIN_YEAR_B <= 1700):
   Date_Class = 2
 elif (MIN_YEAR_B > 1700 and MIN_YEAR_B <= 1749):
    Date_Class = 3
 elif (MIN_YEAR_B < 1600):
    Date_Class = 0

And the argument is:

Date_Class=ReClass(!Date_Class!)

I a m getting a syntax error - not sure why

  • 2
    What exactly is the syntax error you're getting? – munk Dec 2 '14 at 22:11
5

There are a few issues: one has been pointed by recurvata, but there is also an indentation problem (this could be due to formatting only, not to your code, but it needs to be checked) and your code does not return anything.

The argument of your function ReClass() should be MIN_YEAR_B as this is what you use.

def ReClass(MIN_YEAR_B):
    x = -1
    if (MIN_YEAR_B < 1600):
        x = 0 
    elif (MIN_YEAR_B <= 1650):
        x = 1
    elif (MIN_YEAR_B <= 1700):
        x = 2
    elif (MIN_YEAR_B <= 1749):
        x = 3
    else:
        x = 4
    return x

Date_Class=ReClass(!MIN_YEAR_B!)

  • 1
    Much better answer than mine. Only thing I'd change is setting a variable x = 0 before any ifs, which would allow skipping the first if statement. Then set x to 1, 2, or 3 as shown and finally using a single return statement to return x. (Not sure where the 4 above came from). Kind of a matter of style, but I've been scolded that multiple returns don't meet PEP-8 standards. – recurvata Dec 2 '14 at 20:38
  • thanks for the tip, I didn't know about this standard. I've updated my script. – radouxju Dec 2 '14 at 21:48
  • Well, now that I look at the Pep 8 docs, I don't find mention of multiple returns at all. But there is this, docs.python-guide.org/en/latest/writing/style/#returning-values, which seems at least semi-official and discourages most uses of multiple exit returns. Although I've done it myself many times. – recurvata Dec 2 '14 at 22:50
3

You need to pass MIN_YEAR_B in the function as well.

Date_Class=ReClass(!Date_Class!, !MIN_YEAR_B!)

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.