Error "unsupported operand type(s) for +: 'int' and 'NoneType' for variance in python

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'

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]))

• Thanks for the reply! Just tried it. This is what I get. TypeError: float() argument must be a string or a number – netkostas Sep 10 '14 at 11:38
• Just found the problem. The code seems right. The error appears because of wrong input data (Field Elevation contained Null Values) – netkostas Sep 10 '14 at 13:04

Your code snippet is using a variable called path, for which we cannot see the value.

If you insert a print statement as below, I suspect that you will find path has a value other than what you expect when the error is thrown.

If it is set to something that you do expect then at least we will have narrowed down where to look for the error.

print path
with arcpy.da.SearchCursor(path, "Elevation") as sCursor:

• path is a feature class set from arcGIS (e.g. C:/fc.shp) with some fields and "Elevation" as well – netkostas Sep 10 '14 at 7:53
• @netkostas That would be useful information for you to edit into your question, along with the data type of the Elevation field in the first shapefile that your code throws this error on. – PolyGeo Sep 10 '14 at 8:02