0

I am quite new to arcpy. My script seemed to work just fine but I get an error, "the field is not nullable". I guess the problem is with the last block of code but I don't understand why nor how to get around.

Some of the original columns ['T2TC1', 'T2TC2', 'T2TC3','T2TC4','T2TC5'] are equal to 0. No value in my "TOT_Count" column is equal to 0, so I am not dividing by 0.

TOT_Count is the sum of ['T2TC1', 'T2TC2', 'T2TC3','T2TC4','T2TC5']. This seems to be creating the problem but I don't know why. If I replace the values of this column by a random number the problem disapears.

import arcpy
table = "C:\Users\Me\Documents\datas\Count\FinalT4.shp"
arcpy.env.overwriteOutput = True

#input fields with counts#
fields = ['T2TC1', 'T2TC2', 'T2TC3','T2TC4','T2TC5'] # etc.
newFields = []

#create a new f field corresponding to each field#
for field in fields:
    field_name ="F_" + field
    arcpy.AddField_management (table, field_name, "FLOAT")
    newFields.append(field_name)

#for each new field created, calculate the old field divided by the sum#
for newField in newFields:
    for field in fields :
        expr =  '(!{}! / !TOT_Count!)'.format(field)
        arcpy.CalculateField_management (table, newField, expr, 'PYTHON')
  • For each new field you are calculating "old field / sum of all old fields" for each old field. This cant be what you want? – BERA Jun 16 '17 at 5:07
1

I dont know why your code does not work but anyway, you can try this:

I think you should use the da.UpdateCursor instead of the Field Calculator:

UpdateCursor establishes read-write access to records returned from a feature class or table.

Using cursor will give you much more flexibility. Replace your code after adding the fields with:

with arcpy.da.UpdateCursor(table,newFields+fields) as cursor:
    for row in cursor:
        oldsum=sum(row[4:])
        row[0]=row[5]/oldsum
        row[1]=row[6]/oldsum
        row[2]=row[7]/oldsum
        row[3]=row[8]/oldsum
        row[4]=row[9]/oldsum
        cursor.updateRow(row)
1

I think you need to look at your nested for loops. Every 'newField' is going to end up with the exact same number which is 'T2TC5' / TOT_Count.

I'm not sure if this is what is causing your issue as I don't use ArcPy a lot and the 'zone' you mention in the error statement seems a bit odd. But try using this code.

itCount = 0
while (itCount < len(fields):
     expr = '(!{}! / !TOT_Count!)'.format(fields[itCount])
     arcpy.CalculateField_management (table, newFields[itCount], expr, 'PYTHON')
     itCount+=1

Where you have AddField_management(table,field_name,"FLOAT") change it to AddField_management(table,field_name,"FLOAT",field_is_nullab‌​le=True) and see where the null values are being added. Then you can provide more detail on where the error is occuring

  • I was confused, I mean the field is not nullable. I tried your code. It doesn't solve the issue. Apparently it has something to do with my TOT_Count column (which is the sum of the 5 columns ['T2TC1', 'T2TC2', 'T2TC3','T2TC4','T2TC5']). I changed the value of TOT_Count to 100 and then the script runs. – Oh-No Jun 14 '17 at 13:36
  • 1
    Where you have AddField_management(table,field_name,"FLOAT") change it to AddField_management(table,field_name,"FLOAT",field_is_nullable=True) and see where the null values are being added – Liam G Jun 14 '17 at 22:54
  • @Freighty Please edit your answer to add more detail rather than in comments. – Midavalo Jun 16 '17 at 2:01

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.