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

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 2, 2014 at 19:45
1
  • 2
    What exactly is the syntax error you're getting? Commented Dec 2, 2014 at 22:11

2 Answers 2

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!)

nmpeterson
8,42334 silver badges60 bronze badges
answered Dec 2, 2014 at 20:08
3
  • 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. Commented Dec 2, 2014 at 20:38
  • thanks for the tip, I didn't know about this standard. I've updated my script. Commented Dec 2, 2014 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. Commented Dec 2, 2014 at 22:50
3

You need to pass MIN_YEAR_B in the function as well.

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

answered Dec 2, 2014 at 19:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.