ArcGIS Pro field calculator if statements based on value of other field

234
6
Jump to solution
2 weeks ago
Labels (1)
YusefSamari
Occasional Contributor

I am using ArcGIS Pro 3.1.3. I have a field full of dates, stored as a text field, arranged in the format dd/mm/yyyy. I want to extract the month value and store in a newly created field as an integer. If I just put:

!DateField![3:5]

in the field calculator, without using a code block, the new month field becomes populated, like I want it. However, I only want to extract the values from the date field if the value if the criteria in a DateTypeField, which lists the date type of the record (whether it pertains to specific date, or date range e.g. year), are met. Most of the dates are marked "D" in the date type field and these are the ones I want to populate the new month field for. For all other values of DateTypeField, I want the value of the month field to remain null.

My expression in the Code Block looks like this:

def reclass(Date, DateType):
if (DateType == "D"):
Date[3:5]

With correct indentation, which is lost when copying and pasting into this box. The expression passes the validation check. Then I use the following field calculator expression:

reclass(!DateField!,!DateTypeField!)

And it runs, but all values in the table remain NULL. Can anyone see where I am going wrong? Many thanks.

0 Kudos
2 Solutions

Accepted Solutions
DavidSolari
Occasional Contributor III

The field calculator doesn't use any types that are pass-by-reference, so you have to return what you want like so:

 

def reclass(Date, DateType):
    if (DateType == "D"):
        return Date[3:5]
    return None

 

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

This can be done with a Python 6.13. Conditional expressions; Python 3.x documentation, aka ternary operator, and doesn't require a code block:

!DateField![3:5] if !DateTypeField! == "D" else None

View solution in original post

6 Replies
DavidSolari
Occasional Contributor III

The field calculator doesn't use any types that are pass-by-reference, so you have to return what you want like so:

 

def reclass(Date, DateType):
    if (DateType == "D"):
        return Date[3:5]
    return None

 

YusefSamari
Occasional Contributor

Many thanks. Just wondering what you mean by 'pass-by-reference', and also, how do you put a codeblock in the text on here?

0 Kudos
RichardHowe
Occasional Contributor III

The field calculator will also respect a selection and only run on selected rows. So you could do a "select by attributes first (to select records where DateType equals D) and then run your initial calculation

YusefSamari
Occasional Contributor

Thanks. That would work, but I think an if statement incorporated into field calculator would be easier to incorporate into existing models in modelbuilder in this case.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This can be done with a Python 6.13. Conditional expressions; Python 3.x documentation, aka ternary operator, and doesn't require a code block:

!DateField![3:5] if !DateTypeField! == "D" else None
YusefSamari
Occasional Contributor

Nice! Many thanks.

0 Kudos