Copy value from one cell to another, if condition met

832
2
Jump to solution
07-08-2019 11:23 AM
MalcolmLittle
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:

0 Kudos
1 Solution

Accepted Solutions
LanceCole
MVP Regular Contributor

Malcolm, 

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.

View solution in original post

2 Replies
LanceCole
MVP Regular Contributor

Malcolm, 

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.

MalcolmLittle
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.

0 Kudos