[GIS] Error “unsupported operand type(s) for +: ‘int’ and ‘NoneType’ for variance in python

arcgis-10.1arcpynumpy

I recently came up with an error in a script I am working with in Python for ArcGIS 10.1. The strange thing about it is that this script works for some files and not for others.
Any ideas as to why this is happening?

The part of the code I get the error in is:

data_elevation=[]

with arcpy.da.SearchCursor(path, "Elevation") as sCursor:
    for row in sCursor:
        data_elevation.append(row[0])

var_elevation = numpy.var(data_elevation)

path in code stands for a FeatureClass (e.g. C:/test_fc.shp) containing field "Elevation" (e.g. 140, 140, 141, 150, ….).

Error:

Traceback (most recent call last): File "…", line 90, in
var_elevation = numpy.var(data_elevation) File "C:\Python27\ArcGIS10.1\lib\site-packages\numpy\core\fromnumeric.py",
line 2561, in var
return _wrapit(a, 'var', axis, dtype, out, ddof) File "C:\Python27\ArcGIS10.1\lib\site-packages\numpy\core\fromnumeric.py",
line 37, in _wrapit
result = getattr(asarray(obj),method)(*args, **kwds) TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Best Answer

As the script works for some files and not for others, I suspect the problem is originated somewhere in the data (or handling of special cases in the data). TypeError suggest that you may have a mix of data types. I'm not sure how numpy would handle a mix like that. Have you tried to e.g. float the elevation to make sure they are all float numbers?

i.e.something like:

with arcpy.da.SearchCursor(path, "Elevation") as sCursor:
    for row in sCursor:
        data_elevation.append(float(row[0]))