Given a dataset with a date field, how would I select the row with the most recent date using Python or ArcPy?
asked Nov 5, 2014 at 14:38
-
2Here are some approaches that were noted in another post. Note the sections where the OP is wanting the most recent fire: gis.stackexchange.com/questions/95378/…evv_gis– evv_gis2014年11月05日 14:42:26 +00:00Commented Nov 5, 2014 at 14:42
2 Answers 2
Building from evv_gis comment:
- Use search cursor to iterate through layer and assign date values to list
- Use max method on list to get the newest value
- Make feature layer of layer
- Use the select layer by attribute method on feature layer with value from step 2
answered Nov 5, 2014 at 15:11
In the spirit of completeness here is my final script, minus making a feature layer, which I will add in production.
listdates = []
cursor = arcpy.da.SearchCursor(layername, "Date_Mod")
for row in cursor:
listdates.append(row)
maxdate = max(listdates)
maxdatestr = str(maxdate)
datestr = maxdatestr[19:31]
arcpy.SelectLayerByAttribute_management(layername, "NEW_SELECTION", "Date_Mod = date '" + datestr + " 00:00:00'")
Thanks to everyone that answered and commented.
answered Nov 5, 2014 at 16:33
lang-py