title: Speed up pandas iterrows calculation of seasons
body: <pre><code>isSpring = False
  
  # Calculate season e.g. spring wheat season is only ~ May - August
  if isSpring:
      # Change from name of month to a number e.g. January is 1, December is 12
      idx_srt_mon = strptime('May', '%B').tm_mon
      idx_end_mon = strptime('September', '%B').tm_mon
  else:
      idx_srt_mon = strptime('September', '%B').tm_mon
      idx_end_mon = strptime('August', '%B').tm_mon
  
  # Assign seasons, works for crops with season within 1 year and also winter crops with seasons spanning years
  df['Season'] = numpy.nan
  first_yr     = df.YEAR.unique()[0]
  for i,  row in df.iterrows():
      cur_month = row.MONTH
      cur_yr    = row.YEAR
  
      if cur_month &lt; idx_srt_mon:
          if not isSpring and (cur_month &lt; idx_end_mon):
              if cur_yr &gt; first_yr:
                  df.set_value(i, 'Season', cur_yr - 1)
      else:
          if cur_month &lt;= idx_end_mon or not isSpring:
              df.set_value(i, 'Season', cur_yr)
  </code></pre>
  
  <p>In the dataframe available here: <a href="https://www.dropbox.com/s/gwyim6kc83mw3yf/df.csv?dl=0" rel="nofollow">https://www.dropbox.com/s/gwyim6kc83mw3yf/df.csv?dl=0</a>, I would like to add a new column which assigns a 'season' as follows:</p>
  
  <ol>
  <li><p>If isSpring is TRUE, then assign the current year as season to all rows from idx_srt_mon to idx_end_mon (May to September)</p></li>
  <li><p>If isSpring is FALSE, then the season starts in September of current year and goes upto August of next year. For this entire period, spanning across 2 years, the season is the year in which it started. E.g, if the start is September 2000, then the season will be 2000 from September 2000 to August 2001.</p></li>
  <li><p>For all other cases, season is numpy.nan</p></li>
  </ol>
  
  <p>I have used df.iterrows to do this computation, but it is very slow. How can I speed this up?</p>