I found an inconsistency between arcpy in Python 2.7 and in Python 3.4 (ArcGIS 10.4.1 and ArcGIS Pro 1.3.1 Patch 1).
The task is simple. Take an input feature class and a zone feature class, clip the input FC using the zone FC to produce a clipped FC. Then, use the statistics tool to summarize areas (or whatever) by a category field in the clipped FC.
The inconsistency occurs when the the zone feature(s) does not intersect the input feature(s):
I need my script to work in both pythons and empty results is a valuable result too. I dealt with it by checking whether the warning was issued. In my case, the task was inside a for-loop and I could simply skip to the next iteration (using the 'continue' statement) when the warning was raised. The code snippet below shows how one can check if the warning was issued and to act accordingly.
import arcpy geology = r'C:\temp\geology.shp' catchment = r'C:\temp\catchment.shp' arcpy.env.workspace = r'C:\temp\wdb.gdb' stats_table = None clipped = arcpy.analysis.Clip(geology, catchment, 'clppd') warnings = str(clipped.getMessages(1)).lower() if 'empty output generated' in warnings: pass else: clipped_fc = clipped.getOutput(0) stats = arcpy.analysis.Statistics(clipped_fc, 'outtbl', [["Shape_Area", "SUM"]], "LEX") stats_table = stats.getOutput(0)
The same thing happened when I tried it in ArcGIS Pro user interface so it is probably not a Python problem, but I needed to solve it in Python.
I hope this will help someone or that someone will tell me if there is a better way to deal with it.
I agree, not a Python issue. You have done a good job narrowing down the issue. Can you open Esri Support cases? If so, either yourself or through your organization, I suggest doing so to get this bug logged.
You have two inputs as shapefiles and your output is not... did you try to set your clipped data out to a shapefile? This would rule the destination to your workspace as the issue
HI Dan, that's a good tip but the inconsistency occurs even if everything is just in shapefiles.
I'll try to find somebody in my organization who can open Esri Support cases.
So the clipped file wasn't created? Just trying to narrow down were the glitch is.
Another thing I noticed is the way you call the statistics tool... give this form a shot
arcpy.Statistics_analysis(intable, outtable, stats, casefield) (I know, I know... shouldn't make a difference)
And ditto for Clip... also get rid of the getresult stuff and try to actually use the clipped file. If you get rid of all the message/warning trapping, you may get some useful error messages which are far easier to dissect than those awful try/expcept or pass/continue things.