I have a database of wells and water level measurements which I would like to label in a stacked manner with the ID on top and the measurement below it. If the measurement is from 2020, it should appear normally but if it is from before 2020 I want to put the value in parentheses. It seems what I have written so far ignores the else part of the function (everything comes out normal). If I switch the >/< symbol, everything comes out in parentheses.

Does anyone know what I am doing wrong in ArcMap here?

def FindLabel ( [IDMINE], [Date], [Q1_2020_WL] ):
    if [Date] > 12/30/2019:
        return [IDMINE] + '\n' + [Q1_2020_WL]
        return [IDMINE] + '\n' + "(" + [Q1_2020_WL] + ")"
  • 2
    Is this ArcMap or Pro? What is the data type of your date field?
    – BERA
    Apr 22 '20 at 17:37
  • This is in ArcMap and the date field is a String. Displayed as 30-Mar-20 for example.
    – Paramo
    Apr 22 '20 at 17:49

You cant compare strings using ><. Convert your string into a datetimeobject and then "everything" is possible. You need to check Advanced for this to work:

from datetime import datetime
def FindLabel ( [SOMEDATEFIELD], [OBJECTID] ):
    i = [OBJECTID]
    d = datetime.strptime([SOMEDATEFIELD], '%Y-%m-%d') #Adjust to to match your date format, see: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior 
    if d.year < 2016:
        return "{0} \n ({1})".format(i, d.year)
        return "{0} \n {1}".format(i, d.year)

enter image description here

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.