How to Refer to a Null value with ArcPy Expressions












2














I feel like this might be a bit of a simple answer but I can't figure out how to refer to Null values in a field using an expression in python. I've tried using the following as well as = None and =Null but nothing has worked yet:



ScoreCondition = "Cond_Score(!"+Condition+"!)"
codeblock1 = """def Cond_Score(cond):
if cond > 90:
return 1
elif cond >= 71 and cond <= 90:
return 2
elif cond >= 51 and cond <= 70:
return 3
elif cond >= 20 and cond <= 50:
return 4
elif cond < 20:
return 5
elif cond is None:
return 1"""









share|improve this question




















  • 1




    One equals sign is for assigning values. To test for equality, use two of them: ==
    – Dan C
    4 hours ago






  • 1




    can't you just put an Else: for the last elif?
    – klewis
    2 hours ago










  • What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings.
    – Michael Stimson
    2 hours ago
















2














I feel like this might be a bit of a simple answer but I can't figure out how to refer to Null values in a field using an expression in python. I've tried using the following as well as = None and =Null but nothing has worked yet:



ScoreCondition = "Cond_Score(!"+Condition+"!)"
codeblock1 = """def Cond_Score(cond):
if cond > 90:
return 1
elif cond >= 71 and cond <= 90:
return 2
elif cond >= 51 and cond <= 70:
return 3
elif cond >= 20 and cond <= 50:
return 4
elif cond < 20:
return 5
elif cond is None:
return 1"""









share|improve this question




















  • 1




    One equals sign is for assigning values. To test for equality, use two of them: ==
    – Dan C
    4 hours ago






  • 1




    can't you just put an Else: for the last elif?
    – klewis
    2 hours ago










  • What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings.
    – Michael Stimson
    2 hours ago














2












2








2







I feel like this might be a bit of a simple answer but I can't figure out how to refer to Null values in a field using an expression in python. I've tried using the following as well as = None and =Null but nothing has worked yet:



ScoreCondition = "Cond_Score(!"+Condition+"!)"
codeblock1 = """def Cond_Score(cond):
if cond > 90:
return 1
elif cond >= 71 and cond <= 90:
return 2
elif cond >= 51 and cond <= 70:
return 3
elif cond >= 20 and cond <= 50:
return 4
elif cond < 20:
return 5
elif cond is None:
return 1"""









share|improve this question















I feel like this might be a bit of a simple answer but I can't figure out how to refer to Null values in a field using an expression in python. I've tried using the following as well as = None and =Null but nothing has worked yet:



ScoreCondition = "Cond_Score(!"+Condition+"!)"
codeblock1 = """def Cond_Score(cond):
if cond > 90:
return 1
elif cond >= 71 and cond <= 90:
return 2
elif cond >= 51 and cond <= 70:
return 3
elif cond >= 20 and cond <= 50:
return 4
elif cond < 20:
return 5
elif cond is None:
return 1"""






arcpy arcmap field-calculator python-parser null






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









PolyGeo

53.3k1779238




53.3k1779238










asked 5 hours ago









Adam ReedAdam Reed

192




192








  • 1




    One equals sign is for assigning values. To test for equality, use two of them: ==
    – Dan C
    4 hours ago






  • 1




    can't you just put an Else: for the last elif?
    – klewis
    2 hours ago










  • What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings.
    – Michael Stimson
    2 hours ago














  • 1




    One equals sign is for assigning values. To test for equality, use two of them: ==
    – Dan C
    4 hours ago






  • 1




    can't you just put an Else: for the last elif?
    – klewis
    2 hours ago










  • What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings.
    – Michael Stimson
    2 hours ago








1




1




One equals sign is for assigning values. To test for equality, use two of them: ==
– Dan C
4 hours ago




One equals sign is for assigning values. To test for equality, use two of them: ==
– Dan C
4 hours ago




1




1




can't you just put an Else: for the last elif?
– klewis
2 hours ago




can't you just put an Else: for the last elif?
– klewis
2 hours ago












What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings.
– Michael Stimson
2 hours ago




What kind of feature storage is your data? Not every data type can store a None or Null value. Shapefiles can't, numeric Null becomes 0 and string Null becomes '' (an empty string). In geodatabases (personal, file or enterprise) Null values can be stored (test with cond == None as @DanC said) but your implementation of ScoreCondition = "Cond_Score(!"+Condition+"!)" doesn't look quite right, can you show your line where you call Cond_Score in CalculateField_management, it's possible you may be parsing your numbers as strings.
– Michael Stimson
2 hours ago










2 Answers
2






active

oldest

votes


















5














Your problem is that in Python 2 None is less than everything.



>>> None < 20
True
>>> None < numpy.nan
True
>>> None < float('-inf')
True
>>> None < 'Everything'
True


Notes:




  • In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue.

  • This doesn't apply to shapefiles as the old dBase format .dbf that stores the attribute table doesn't support null values


In your expression, Python is returning True when evaluating the
elif cond < 20 clause when cond IS really None so never gets to the elif cond is None: clause.



So the following will work because it tests for None first:



def Cond_Score(cond):
if cond is None:
return 1
elif cond > 90:
return 1
elif cond >= 71 and cond <= 90:
return 2
elif cond >= 51 and cond <= 70:
return 3
elif cond >= 20 and cond <= 50:
return 4
elif cond < 20:
return 5





share|improve this answer































    0














    I've confirmed all three of the following work when using the Field Calculator. Could it be an error somewhere else? Does the code work for the other condition?



    if cond == None:
    return 1
    if cond is None:
    return 2
    if not cond:
    return 3





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "79"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f308272%2fhow-to-refer-to-a-null-value-with-arcpy-expressions%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      Your problem is that in Python 2 None is less than everything.



      >>> None < 20
      True
      >>> None < numpy.nan
      True
      >>> None < float('-inf')
      True
      >>> None < 'Everything'
      True


      Notes:




      • In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue.

      • This doesn't apply to shapefiles as the old dBase format .dbf that stores the attribute table doesn't support null values


      In your expression, Python is returning True when evaluating the
      elif cond < 20 clause when cond IS really None so never gets to the elif cond is None: clause.



      So the following will work because it tests for None first:



      def Cond_Score(cond):
      if cond is None:
      return 1
      elif cond > 90:
      return 1
      elif cond >= 71 and cond <= 90:
      return 2
      elif cond >= 51 and cond <= 70:
      return 3
      elif cond >= 20 and cond <= 50:
      return 4
      elif cond < 20:
      return 5





      share|improve this answer




























        5














        Your problem is that in Python 2 None is less than everything.



        >>> None < 20
        True
        >>> None < numpy.nan
        True
        >>> None < float('-inf')
        True
        >>> None < 'Everything'
        True


        Notes:




        • In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue.

        • This doesn't apply to shapefiles as the old dBase format .dbf that stores the attribute table doesn't support null values


        In your expression, Python is returning True when evaluating the
        elif cond < 20 clause when cond IS really None so never gets to the elif cond is None: clause.



        So the following will work because it tests for None first:



        def Cond_Score(cond):
        if cond is None:
        return 1
        elif cond > 90:
        return 1
        elif cond >= 71 and cond <= 90:
        return 2
        elif cond >= 51 and cond <= 70:
        return 3
        elif cond >= 20 and cond <= 50:
        return 4
        elif cond < 20:
        return 5





        share|improve this answer


























          5












          5








          5






          Your problem is that in Python 2 None is less than everything.



          >>> None < 20
          True
          >>> None < numpy.nan
          True
          >>> None < float('-inf')
          True
          >>> None < 'Everything'
          True


          Notes:




          • In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue.

          • This doesn't apply to shapefiles as the old dBase format .dbf that stores the attribute table doesn't support null values


          In your expression, Python is returning True when evaluating the
          elif cond < 20 clause when cond IS really None so never gets to the elif cond is None: clause.



          So the following will work because it tests for None first:



          def Cond_Score(cond):
          if cond is None:
          return 1
          elif cond > 90:
          return 1
          elif cond >= 71 and cond <= 90:
          return 2
          elif cond >= 51 and cond <= 70:
          return 3
          elif cond >= 20 and cond <= 50:
          return 4
          elif cond < 20:
          return 5





          share|improve this answer














          Your problem is that in Python 2 None is less than everything.



          >>> None < 20
          True
          >>> None < numpy.nan
          True
          >>> None < float('-inf')
          True
          >>> None < 'Everything'
          True


          Notes:




          • In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue.

          • This doesn't apply to shapefiles as the old dBase format .dbf that stores the attribute table doesn't support null values


          In your expression, Python is returning True when evaluating the
          elif cond < 20 clause when cond IS really None so never gets to the elif cond is None: clause.



          So the following will work because it tests for None first:



          def Cond_Score(cond):
          if cond is None:
          return 1
          elif cond > 90:
          return 1
          elif cond >= 71 and cond <= 90:
          return 2
          elif cond >= 51 and cond <= 70:
          return 3
          elif cond >= 20 and cond <= 50:
          return 4
          elif cond < 20:
          return 5






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 13 mins ago

























          answered 3 hours ago









          LukeLuke

          28.8k252102




          28.8k252102

























              0














              I've confirmed all three of the following work when using the Field Calculator. Could it be an error somewhere else? Does the code work for the other condition?



              if cond == None:
              return 1
              if cond is None:
              return 2
              if not cond:
              return 3





              share|improve this answer


























                0














                I've confirmed all three of the following work when using the Field Calculator. Could it be an error somewhere else? Does the code work for the other condition?



                if cond == None:
                return 1
                if cond is None:
                return 2
                if not cond:
                return 3





                share|improve this answer
























                  0












                  0








                  0






                  I've confirmed all three of the following work when using the Field Calculator. Could it be an error somewhere else? Does the code work for the other condition?



                  if cond == None:
                  return 1
                  if cond is None:
                  return 2
                  if not cond:
                  return 3





                  share|improve this answer












                  I've confirmed all three of the following work when using the Field Calculator. Could it be an error somewhere else? Does the code work for the other condition?



                  if cond == None:
                  return 1
                  if cond is None:
                  return 2
                  if not cond:
                  return 3






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 3 hours ago









                  MBuieMBuie

                  91




                  91






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f308272%2fhow-to-refer-to-a-null-value-with-arcpy-expressions%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List directoties down one level, excluding some named directories and files

                      list processes belonging to a network namespace

                      list systemd RuntimeDirectory mounts