Select to view content in your preferred language

Python script field calculator skipping rows

942
4
02-26-2014 08:38 AM
FernBraun1
New Contributor
I built a Python tool that uses Field Calculator to add and calculate a few different fields in a shapefile, including area as well as a few simple string variables based on user input. The function throws no errors and works fine, except that it seems to randomly skip rows, leaving zeroes (for numbers) or blanks (for string variables) instead of calculating the values. The blank/zero rows tend to come in big chunks, not scattered, but other than that there is no obvious pattern.

If I open up the output shapefile in ArcGIS and calculate those same fields using the Field Calculator or Calculate Geometry tools, the zeroed/blank values calculate without a problem, so it isn't a problem with the shapefile itself being corrupt. I have tried the script over and over with a variety of shapefiles, and while it occasionally works, 95% of the time at least some rows are skipped.

I'm curious if anyone has experienced this problem before or might have suggestions for solutions.
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Fern,

Any way you could use a File Geodatabase feature class?  I try to avoid working with shapefiles unless it's absolutely necessary.  You can run a quick test by importing the shapefile into a FGD and see if  you receive the same results from the script.
0 Kudos
MattEiben
Deactivated User
Any way you can provide the code for the problem section of the script?
0 Kudos
ClintDow
Deactivated User
Is there any selection present limiting which rows will be calculated?

Alternatively have you tried using an UpdateCursor instead of CalculateField_management? To me this seems to be an easier to follow way of doing the same thing, but its a case of personal preference I suppose.
0 Kudos
NickJacob
Deactivated User
I've had similar problems with the Calculate Field Tool, especially when working with string/text manipulations.  I'm guessing the zeros described in your shapefile are related to geodatabase rows without values.  In other words, the expression or function, isn't returning anything.  A good starting place may be to check the indentation in your function/codeblock if you defined one in a docstring using triple quotes.  Experimenting in ArcMap's Python Window is a great way to troubleshoot.

I'd also second clint.dow's recommendation to adopt Update Cursors in place of the old field calculator approach.  The newer data access module in particular may be of interest because you can reference field names in a slightly different way which would give you another angle to work with.

Just curious.  Did you find a solution?
0 Kudos