1

I need to sum values of 2 columns in field calculator and populate a new column with the result, one of the values always in Null. I already had found a useful answer In here, but it doesn't help with the last couple of columns, that contain a date in DD.MM.YYYY (string) format- geoprocessing report shows successful operation, but the new column stays empty. Is there a small tweak i could use for the code, to make it work?

def stack(Y, Y_1): itemList = [Y, Y_1] myList = [Y for Y in itemList if (Y != None)] return sum(myList)

Field value= stack( !Y!, !Y_1!)

Data table

  • Can you add your code? – MacroZED Sep 30 '16 at 10:31
  • Added the code to the question. – User6819 Sep 30 '16 at 10:35
  • Have you tried converting the Nulls to 0 and then trying to calculate the fields? Try if Y is None, return 0. Then go ahead and try your calculation again – MacroZED Sep 30 '16 at 10:39
  • Can you also show the table with the fields and what type of data it has? – MacroZED Sep 30 '16 at 10:40
  • I haven't tried converting Nulls to 0, but the code worked well with Nulls, when calculating double format data, these are string. – User6819 Sep 30 '16 at 10:46
2

Based on your comments, it sounds like sum is not quite the right name for the field. You want to coalesce two fields.

Field value = !Y! or !Y_1!

This post on StackOverflow can give you more background on coalesce.

0

Try the below. It looks into the first column (date) and if there nothing in it, will return that into the sum field. And vice versa for the date_1 field.

def stack (date, date_1, date_sum):
    if date is None:
        return date_1
    else:
        return date

date_sum = stack (!date!, !date_1!, !date_sum!)
  • This code produced error 000989 on line2. – User6819 Sep 30 '16 at 11:23
  • Have you tried the edited version? Ok – MacroZED Sep 30 '16 at 11:27
  • Sorry, the code still gives an error, but Richard's suggestion with coalesce did the trick! – User6819 Sep 30 '16 at 11:55

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.