# Copy value from one cell to another, if condition met

629
2
07-08-2019 11:23 AM
New Contributor III

Hello,

I'm trying some field calculation Python code that would copy over a corresponding value from one float field to another float field, based on whether a Date field is a specific date. Essentially, I want temperature values (Jan26_temp) copied over to another field (Pin_temp) only if the FullDateTime field's date is January 26, 2019. Unfortunately, my code returns only Null values. Here is my code:

Tags (2)
1 Solution

Accepted Solutions
MVP Regular Contributor

When calling a function to assign a value to an attribute in field calculator you need to pass the parameters needed in the function.  In this case, the pinning function needs to pass the datetime (FullDateTime) and the temperature (Jan26_temp) so your function call should be pinning(!FullDateTime!, !Jan26_temp!)  These will be passed to the function as the variables defined in the def statement.  In the case below, FullDateTime is assigned to dt and Jan26_temp is assigned to temp.  You then use dt and temp in your function to perform your calculations/comparisons and return a value or variable using a return statement, in this case the value of temp or -99.

``````def pinning(dt, temp):
if dt.strftime("%Y/%m/%d") == "2019/01/26":
return temp
else:
return -99 ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

Working with dates in ArcGIS is temperamental.  What I have done above is used the strftime method of a datetime class to return a string representing the date in the format of YYYY/MM/DD and compare it to the string "2019/01/26", the date you are checking.

Another method you could use would be to create a datetime for 1/26/2019 and check the date portion of this value to the date portion of your FullDateTime

``````chk = datetime.datetime(2019,1,26)
def pinning(dt, tmp):
if dt.date() == chk.date():
return tmp
else:
return -99‍‍‍‍‍‍‍‍‍‍‍‍``````

I hope that helps.

2 Replies
MVP Regular Contributor

When calling a function to assign a value to an attribute in field calculator you need to pass the parameters needed in the function.  In this case, the pinning function needs to pass the datetime (FullDateTime) and the temperature (Jan26_temp) so your function call should be pinning(!FullDateTime!, !Jan26_temp!)  These will be passed to the function as the variables defined in the def statement.  In the case below, FullDateTime is assigned to dt and Jan26_temp is assigned to temp.  You then use dt and temp in your function to perform your calculations/comparisons and return a value or variable using a return statement, in this case the value of temp or -99.

``````def pinning(dt, temp):
if dt.strftime("%Y/%m/%d") == "2019/01/26":
return temp
else:
return -99 ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

Working with dates in ArcGIS is temperamental.  What I have done above is used the strftime method of a datetime class to return a string representing the date in the format of YYYY/MM/DD and compare it to the string "2019/01/26", the date you are checking.

Another method you could use would be to create a datetime for 1/26/2019 and check the date portion of this value to the date portion of your FullDateTime

``````chk = datetime.datetime(2019,1,26)
def pinning(dt, tmp):
if dt.date() == chk.date():
return tmp
else:
return -99‍‍‍‍‍‍‍‍‍‍‍‍``````

I hope that helps.

New Contributor III

Thanks for the clarification, Lance. I actually modified the else: statement to send pass instead of -99, seeing that I will need to pin the temperature data for a total of 12 data collection days.