<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Converting Arcade Script to Python in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511772#M86127</link>
    <description>&lt;P&gt;Thanks for the suggestion! I've tried something similar (I've edited my post above to show the code) and I'm getting this error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;TypeError&lt;/SPAN&gt;: unsupported operand type(s) for -: 'datetime.datetime' and 'SList'&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jul 2024 17:13:21 GMT</pubDate>
    <dc:creator>CaitlinSjodin</dc:creator>
    <dc:date>2024-07-29T17:13:21Z</dc:date>
    <item>
      <title>Converting Arcade Script to Python</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511755#M86124</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;This feels like a problem that should have a really straightforward solution but for some reason I can't wrap my head around it. I have a simple arcade script that was given to me that was used in the Calculate Field window. I'm trying to take that expression and incorporate it into a python script in jupyter notebook.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the arcade script:&lt;/P&gt;&lt;P&gt;var LINED = $feature.LINED&lt;BR /&gt;var startDate = IIf(LINED == "Yes", Date($feature.LINEDYEAR), Date($feature.INSTALLDATE));&lt;BR /&gt;var endDate = Now();&lt;BR /&gt;var age = round(DateDiff(endDate, startDate, 'years'),2);&lt;BR /&gt;return age;&lt;/P&gt;&lt;P&gt;It's being used to populate a field called AGE_YEARS&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically I need a python script that says:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;If (LINED == 'Yes'):
# Calculate AGE_YEARS to be the difference between LINEDYEAR and the current date
else:
# Calculate AGE_YEARS to be the difference between INSTALLYEAR and the current date

# Below is what I have so far

from datetime import datetime

edit_layer = "AIRDRIE_EDIT.GIS.sGravityMain"

LinedDate = !LINED_YEAR!
InstallDate = !INSTALL_DATE!
CurrentDate = datetime.now()

diff1 = CurrentDate - LinedDate
diff2 = CurrentDate - InstallDate

AgeYears = "getClass(!LINED!)"

# Criteria for how to calculate field
codeblock = """
def getClass(LINED):
    if (LINED == 'Yes'):
        return diff1
    else:
        return diff2"""

# calculate age in years
arcpy.management.CalculateField(edit_layer, 'AGE_YEARS', AgeYears, "PYTHON3", codeblock)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above block shows what I have so far. Any suggestions are appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 17:05:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511755#M86124</guid>
      <dc:creator>CaitlinSjodin</dc:creator>
      <dc:date>2024-07-29T17:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Arcade Script to Python</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511771#M86126</link>
      <description>&lt;P&gt;This might help&amp;nbsp;&lt;FONT size="4" color="#000000"&gt;&lt;A href="https://gis.stackexchange.com/questions/462935/populating-field-based-on-value-of-another-field-using-boolean-or" target="_self"&gt;Populating field based on value of another field&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4" color="#000000"&gt;Maybe something like the following:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;AGE_YEARS = reclass(!LINED!)

def reclass(LINED):
    if(LINED == "YES":
        return !LINEDYEAR! - !CURRENTDATE!
    else:
        return !INSTALLYEAR! - !CURRENTDATE!&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 17:06:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511771#M86126</guid>
      <dc:creator>Ed_</dc:creator>
      <dc:date>2024-07-29T17:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Arcade Script to Python</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511772#M86127</link>
      <description>&lt;P&gt;Thanks for the suggestion! I've tried something similar (I've edited my post above to show the code) and I'm getting this error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;TypeError&lt;/SPAN&gt;: unsupported operand type(s) for -: 'datetime.datetime' and 'SList'&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 17:13:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511772#M86127</guid>
      <dc:creator>CaitlinSjodin</dc:creator>
      <dc:date>2024-07-29T17:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Arcade Script to Python</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511778#M86129</link>
      <description>&lt;P&gt;That's a datatype support error&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe look into &lt;A href="https://stackoverflow.com/questions/50816417/how-to-calculate-number-of-years-between-two-dates-in-different-pandas-columns" target="_self"&gt;this&lt;/A&gt;&amp;nbsp; and &lt;A href="https://stackoverflow.com/questions/72153849/unsupported-operand-types-for-datetime-date-and-timestamp" target="_self"&gt;this&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 17:29:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511778#M86129</guid>
      <dc:creator>Ed_</dc:creator>
      <dc:date>2024-07-29T17:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Arcade Script to Python</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511885#M86140</link>
      <description>&lt;P&gt;Thanks for the help, the issue seemed to be that the script wasn't recognizing the field name as the field, instead it seemed to just be reading it as a string. I couldn't figure it out and I ended up going down a rabbit hole that lead me to using an updatecursor instead. I've listed the code below, hopefully it helps someone.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from datetime import datetime

# Layer being edited 
edit_layer = "SanMain"

# Fields being used in the updatecursor
fields = ['LINEDYEAR', 'INSTALLDATE', 'AGE_YEARS', 'LINED']

#Variable showing current date
CurrentDate = datetime.now()

# Update cursor to run through layer and update the AGE_YEARS field with the difference between now and lined or install date
with arcpy.da.UpdateCursor(edit_layer, fields) as cursor:
    for row in cursor:
        if (row[3] == 'Yes'):
            row[2] = round(((CurrentDate - row[0]).total_seconds())/(365.25*24*60*60),2)
        elif (row[3] == 'No' or row[3]== None):
            row[2] = round(((CurrentDate - row[1]).total_seconds())/(365.25*24*60*60),2)

        cursor.updateRow(row)&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 29 Jul 2024 20:31:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511885#M86140</guid>
      <dc:creator>CaitlinSjodin</dc:creator>
      <dc:date>2024-07-29T20:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Arcade Script to Python</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511923#M86147</link>
      <description>&lt;P&gt;I suspect the solution could be using an update cursor (&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/updatecursor-class.htm" target="_self"&gt;update cursor documentation)&lt;/A&gt;&amp;nbsp; - that will go row by row through your data, evaluate the conditions you are testing for, then apply a change in the target field. The form of this would look something like this, this is very back of the napkin sketched out here, so you'll have to work through it and there are probably much more elegant ways to handle this.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fc = path name to your feature class to update
fields = list of fields you need ['LINED','AGE_YEARS']
with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
       if row[0]== 'Yes':
          lined_date = &amp;lt;calc to get diff1 in your example&amp;gt;
          row[1] = lined_date
       elif row[0]&amp;lt;&amp;gt; 'Yes':
          lined_date = &amp;lt;calc to get diff2 in your example&amp;gt;
          row[1] = lined_date
       cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 21:23:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/converting-arcade-script-to-python/m-p/1511923#M86147</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2024-07-29T21:23:17Z</dc:date>
    </item>
  </channel>
</rss>

