2

I'm somewhat familiar with ArcGIS Desktop, but have no experience in Python. I have about 100 shapefiles which have an attribute table containing a date, but I need a column which contains a corresponding season.

To complicate things a little more, I do not want to define seasons the traditional way. Seasons are defined as follows: a. Summer = June, July, August, September b. Fall = October, November c. Winter = December, January, February, March d. Spring = April, May

So far, I've been pasting and copying each attribute table into Excel, and using a function that looks like this: =CHOOSE(MONTH(J2),"Winter","Winter","Winter","Spring","Spring","Summer","Summer","Summer","Summer","Fall","Fall","Winter")

I'm sure there is a faster way to do things.


I recently added a 'MONTH' column using this script: Left( [GMT_DATE] , Len( [GMT_DATE] ) -8 )


I figured out how to generate a season column from a month column, but I am still unable to generate a season column directly from a date column. When I try I get "Winter" for everything.

enter image description here

  • 1
    i dont have time to write the code out, but in my head the pseudocode would be, if you have a field with the date, you can use split on "/" to get only the month, then it would just be a simple if-then, if month = 12,1,2,3 then season = "winter" , if month = 4,5, then season "spring" etc... I think there are some date functions that may do that automatically, but this will def work. – ed.hank May 3 '17 at 15:10
  • Is this in VB Script or Python? I've separated the month out into a separate column as you suggested, but can't seem to get the if-then statement to work. I just need help on syntax. – J.W. Powell May 3 '17 at 15:26
  • Please edit the question with your additional work. – Bjorn May 3 '17 at 15:29
  • Are you opposed to using R? I have a code that does exactly this already. – GISKid May 3 '17 at 15:32
  • 1
    You say "can't seem to get the if-then statement to work. I just need help on syntax.", what does your code look like so far? – Dan C May 3 '17 at 15:41
5

You would have to use the code blocks. Here is an example of how it would work. I tested it and it works

Expression:

    getSeason(!MONTH!) #Or the name of your field for months

CodeBlock:

def getSeason(month):
    if (month == "DECEMBER" or month == "JANUARY" or month == "FEBRUARY" or month == "MARCH"):
       return "WINTER"
    elif(month == "APRIL" or month == "MAY"):
       return "SPRING"
    elif(month =="JUNE" or month=="JULY" or month == "AUGUST" or month == "SEPTEMBER"):
       return "SUMMER"
    else:
       return "FALL"

EDIT: Of course you can use the date field

expression:

 getSeason(!DATE!)

CodeBlock

 def getSeason(date):
    month = int(date.split("/")[0])
    if (month > 11 or month <= 3):
       return "WINTER"
    elif (month == 4 or month == 5):
       return "SPRING"
    elif (month >=6 and month <= 9):
       return "SUMMER"
    else:
       return "FALL"
  • 1
    Thank you! That worked perfectly. Do you know if there is a way to do this without creating a 'MONTH' column.? i.e. Use a similar code, but use a date field with the format MM/DD/YYYY, – J.W. Powell May 3 '17 at 16:15
  • 1
    Sure, I edited the answer! – Jonathan Gaudreau May 3 '17 at 17:01
  • 1
    I appreciate the help Jonathan. For some reason I'm getting "WINTER" for every season when I try and use your second code using only the date field. – J.W. Powell May 3 '17 at 17:19
  • 1
    That was all that was needed to fix it. Thanks Ed. – J.W. Powell May 3 '17 at 18:23
  • 1
    Oh, yeah sorry about that! I corrected the solution by adding int() in the getSeason function. – Jonathan Gaudreau May 3 '17 at 18:25
-1

In R using the lubridate package:

library(lubridate)

Birds<-read.csv("NearBirdsClean.csv", header=TRUE, stringsAsFactors = FALSE)

#date format 2010-08-21 YYYY-MM-DD
 #create dates variable for your column that contains dates 
dates <- Birds$Date_Found
#get the month of the date, create new column called month
Birds$month<-(month(dates, label=TRUE))
#new column called season
season <- Birds$season
#divide up your seasons by month
winter <-c("Dec","Jan", "Feb")
spring <- c("Apr", "May","Mar")
summer <- c("Jun", "Jul", "Aug")
fall <- c("Sep","Oct", "Nov")


#reformat to work with grep
winter <- paste(winter,collapse="|")
spring <- paste(spring,collapse="|")
summer<- paste(summer,collapse="|")
spring <- paste(spring,collapse="|")
fall <- paste(fall,collapse="|")
#populate the season field based on matching seasons (above) in the month field
season[grep(winter, Birds$month)] <- "winter"
season[grep(spring, Birds$month)] <- "spring"
season[grep(summer, Birds$month)] <- "summer"

season[grep(fall, nearBats$month)] <- "fall"
head(Birds)

  Date_Found month seasonB
1 2010-01-19   Jan  winter
2 2010-01-04   Jan  winter
3 2009-12-27   Dec  winter
4 2010-01-01   Jan  winter
5 2010-01-06   Jan  winter
6 2010-01-21   Jan  winter
  • Could the downvoter explain why? OP clarified in the comments they wanted an R solution... – GISKid Sep 25 '17 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.