title: How to Calculate Dry and Wet Spell in Python?
body: <p>I have a random time series data with four columns like: year, month, day, precipitation. I want to calculate dry/wet spell for different spell-length. I am looking for a more convenient way to do that while currently doing with some ugly codes like below:</p>
  
  <pre><code>import numpy as np
  data = np.loadtxt('Data Series.txt', usecols=(1,3))
  dry = np.zeros(12)
  wet = np.zeros(12)
  
  rows,cols = data.shape #reading number of rows and columns into variables
  
  for i in xrange (0,rows):
      for m in xrange(0,12):
          if data[i,1] == 0 and data[i-1,1] == 0 and data[i-2,1] == 0:
              if data[i,0] == m+1:
                  dry[m] += 1.0
          if data[i,1] &gt; 0 and data[i-1,1] &gt; 0 and data[i-2,1] &gt; 0:
              if data[i,0] == m+1:
                  wet[m] += 1.0
  print '3 Days Dry Spell\n', dry
  print '3 Days Wet Spell\n', wet
  </code></pre>
  
  <p>Now, if I want to calculate the 4,5,6 days spells, than "if data[i,1] == 0 and data[i-1,1] == 0....." becomes a huge one. Can anyone please help me so that I may give the spell-length only instead of this long-ugly line?</p>