3

I am trying to assign values to a "K" field in an attribute table based on the "gridcode" values. However, I get this error:

"ERROR 999999: Error executing function. The value type is incompatible with the field type. [K] Failed to execute (CalculateField)"

This is my code:

def C(x):
  if (x == 4260)and(x == 4252)and(x == 11110)and(x == 11817)and(x == 11857):
    return 0.05
  elif (x == 11705)and(x == 11711)and(x == 11730)and(x == 11765):
    return 0.12
  elif (x == 4265)and(x == 4267)and(x == 4284)and(x == 4414)and(x == 4464)and(x == 11052)and(x == 11767)and(x == 11771)and(x == 11772)and(x == 11775)and(x == 11779)and(x == 11780)and(x == 11783)and(x == 11786)and(x == 11791)and(x == 11793)and(x == 11796)and(x == 11798)and(x == 11799)and(x == 11801)and(x == 11807)and(x == 11809)and(x == 11810)and(x == 11811)and(x == 11814)and(x == 11815)and(x == 11819)and(x == 11821):
    return 0.2
  elif (x == 4324)and(x == 4325)and(x == 4396)and(x == 4486)and(x == 4491)and(x == 4494)and(x == 4499)and(x == 4502)and(x == 4522)and(x == 4527)and(x == 4544)and(x == 4552)and(x == 4587)and(x == 4588)and(x == 6651)and(x == 11013)and(x == 11366)and(x == 11368)and(x == 11369)and(x == 11535)and(x == 11766)and(x == 11770)and(x == 11773)and(x == 11777)and(x == 11782)and(x == 11784)and(x == 11785)and(x == 11787)and(x == 11788)and(x == 11789)and(x == 11790)and(x == 11792)and(x == 11794)and(x == 11795)and(x == 11797)and(x == 11800)and(x == 11804)and(x == 11805)and(x == 11806)and(x == 11808)and(x == 118012)and(x == 11816)and(x == 11818)and(x == 11830)and(x == 11868):
    return 0.21
  elif (x == 4408)and(x == 11841)and(x == 11843)and(x == 11847):
    return 0.24
  elif (x == 4383)and(x == 11108)and(x == 11367)and(x == 11371)and(x == 11375)and(x == 11376)and(x == 11378)and(x == 11404)and(x == 11413)and(x == 11416)and(x == 11423)and(x == 11539)and(x == 11540)and(x == 11708)and(x == 11710)and(x == 11714)and(x == 11719)and(x == 11721)and(x == 11723)and(x == 11724)and(x == 11725)and(x == 11839)and(x == 11840)and(x == 11842)and(x == 11844)and(x == 11846)and(x == 11851):
    return 0.26
  elif (x == 11551):
    return 0.28
  elif (x == 4261)and(x == 4264)and(x == 4393)and(x == 11000)and(x == 11014)and(x == 11020)and(x == 11035)and(x == 11086)and(x == 11087)and(x == 11111)and(x == 11262)and(x == 11341)and(x == 11377)and(x == 11388)and(x == 11604)and(x == 11605)and(x == 11627)and(x == 11645)and(x == 11663)and(x == 11668)and(x == 11727)and(x == 11826)and(x == 11827)and(x == 11828)and(x == 11831)and(x == 11858)and(x == 11864)and(x == 11865)and(x == 11869)and(x == 11879)and(x == 11880)and(x == 11883)and(x == 11909)and(x == 11914):
    return 0.34
  elif (x == 11103)and(x == 11916):
    return 0.37
  else:
    return "NULL"

I also attach a screenshot of the field calculator screen for more clarity.

enter image description here

4
  • 2
    It appears you are updating a real number field, but you are returning text, "NULL", which is likely causing the incompatible field issue. If you want NULL in the table, you should return Python None.
    – bixb0012
    Mar 12, 2023 at 19:30
  • @bixb0012 That's true, thx!
    – Ghaith
    Mar 13, 2023 at 18:54
  • @BERA It's or indeed. Sorry, am still new to Python
    – Ghaith
    Mar 13, 2023 at 18:55
  • 1
    @BERA haha thx a lot for your help
    – Ghaith
    Mar 13, 2023 at 18:58

1 Answer 1

10

Assuming your K field is a float or double type, you are returning a text string "NULL" from your else: clause and you can't store a string in either type. As @bixb0012 notes, if you want NULLs, return Python None.

E.g

else:
    return None

However, your expression is flawed and will always return None as you test if a value is equal to a number and another number at the same time which is impossible. E.g x can never be 1 AND 2. It can only be 1 OR 2.

So use or:

def C(x):
  if (x == 4260) or (x == 4252) or (x == 11110) or (x == 11817) or (x == 11857):
    return 0.05
  etc...
  else:
    return None

Even better, use the in operator:

def C(x):
  if x in (4260, 4252, 11110, 11817, 11857):
    return 0.05
  etc...
  else:
    return None
1
  • Thx a lot. Problem solved :D
    – Ghaith
    Mar 13, 2023 at 18:56

Your Answer

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

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