arcpy: maximum of a field of a feature class

389
11
04-26-2019 05:34 AM
RamB
by
Occasional Contributor II

Hi

I would like to get the maximum of a field in a feature class.  If no features are selected, I get the right value. But if some features are selected, I am getting the maximum of a field from within these selected features. However, I would like to get maximum of a field from the whole table i.e., from both selected and un-selected features. 

how may I do it?

Thank you

Reply
0 Kudos
11 Replies
LukeWebb
Occasional Contributor III

Dans Answer is correct.

in_fc = 'C:/Your_spaceless_path/CoordGeom.gdb/Polygons'

arr = arcpy.da.TableToNumPyArray(in_fc, 'Shape_Area')  # pick a field

# nan functions account for nulls, cast to the dtype of the field using astype
np.nanmax(arr.astype('float')) 

155.0

If this is returning different numbers based on selection, then this is because you are not entering "in_fc" as shwon above.

E.g.

in_fc = 'C:/Your_spaceless_path/CoordGeom.gdb/Polygons'

Is different than:

in_fc = 'Polygons'

One points to a layer in a map, and will return results based on selections and things, the other points to data on a disk, and will return counts of whatever is inside that dataset.

The only possible thing that could go wrong using this, is if a user is inside an Edit session, then there features are not saved to the "dataset on disk" until they save Edits.

RamB
by
Occasional Contributor II

Thank you Dan PattersonJoshua BixbyLuke Webb 

I did two different approaches and both worked.

Approach 1. I first got a max for the selection, then switched the selection and got a second max, then took max of the both. I felt it was slow, but it works and this switching also updated my labels in display, which kind of confirmed the display of new max assigned to the features.

Approach 2. I did not follow the advice of giving a  hard coded path, because user can have the file anywhere on the system. So instead, I used layer.dataSource to get the path.  Then I used np.amax to get the max of the array (attribute table). 

So broadly I followed the advice of path and the advice of np.array and also liked the list comprehension technique by Joshua. that also works.

Reply
0 Kudos