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?
Dans Answer is correct.
arr arcpy da TableToNumPyArray in_fc
np nanmax arr astype
If this is returning different numbers based on selection, then this is because you are not entering "in_fc" as shwon above.
Is different than:
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.
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.