SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import arcpy
  2. from arcpy import env
  3. env.workspace = 'the database'
  4. butterfly = r'butterfly_2019' # the feature class
  5.  
  6. for i in butterfly['ID']:
  7.     d1 = datetime.date.max('Datum')
  8.     d2 = datetime.date.min('Datum')
  9.     countdays = d2-d1
  10.     butterfly['NrDays'] = antal
  11.  
  12. out_location = 'the database'
  13. arcpy.FeatureClassToGeodatabase_conversion(butterfly , out_location)
  14.      
  15. import arcpy
  16. from collections import defaultdict
  17.  
  18. fc = r'C:data.gdbsomefc'
  19. datefield = 'DATE'
  20. groupfield = 'GROUP'
  21.  
  22. #Create dictionary
  23. d = defaultdict(list)
  24. with arcpy.da.SearchCursor(fc,[groupfield,datefield]) as cursor:
  25.     for group, date in cursor:
  26.         d[group].append(date)
  27. #d can now look like: defaultdict(<class 'list'>, {'OXFLYET': [datetime.datetime(2011, 4, 1, 0, 0), datetime.datetime(2011, 4, 1, 0, 0), datetime.datetime(2016, 11, 15, 0, 0), datetime.datetime(2011, 5, 1, 0, 0), datetime.datetime(2011, 5, 1, 0, 0)], 'MÖGSJÖN': [datetime.datetime(1992, 9, 1, 0, 0), None, None, datetime.datetime(1994, 1, 1, 0, 0)]})
  28. #Each group is key (I only have two), and all dates are values in a list
  29.  
  30. #Calculate delta days per group
  31. for group, datelist in d.items():
  32.     datelist = [x for x in datelist if x is not None]
  33.     if len(datelist) > 1:
  34.         print('{0}: {1} days'.format(group, (max(datelist)-min(datelist)).days))
  35.     else:
  36.         print('{0}: not enough dates'.format(group))
  37.      
  38. OXFLYET: 2055 days
  39. MÖGSJÖN: 487 days
  40.      
  41. df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,[groupfield,datefield]), columns=[groupfield,datefield])
  42. print(df.groupby(groupfield)[datefield].apply(lambda g: g.max() - g.min()))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top