Add column to dataframe based on date column range
<p>I have a <code>df</code> containing <code>n</code> <code>columns</code>. One of these is a <code>column</code> named <code>date</code> which contains values formatted as <code>mm-dd-yy</code>. Now I want to add a <code>column</code> <code>interval</code> to my <code>df</code>. This <code>column</code> should return the <code>year</code> contained in <code>date</code> but also if it's <code>H1</code> or <code>H2</code>. <code>H1</code> is half year one and should be all <code>date</code> values between <code>01-dd-yy</code> <code>06-dd-yy</code> and thus <code>H2</code> should be all <code>date</code> values between <code>07-dd-yy</code> and <code>12-dd-yy</code>.</p>

<p>This is an example of the data in <code>df['date']</code>:</p>

<pre><code>0         01-27-16
1         02-27-16
2         03-27-16
3         04-27-16
4         05-27-16
5         06-27-16
6         07-27-16
7         08-24-16
8         09-24-16
9         10-16-15
...etc...
</code></pre>

<p>In <code>df</code> I want to add another column named <code>interval</code> containing:</p>

<pre><code>    0         16H1
    1         16H1
    2         16H1
    3         16H1
    4         16H1
    5         16H1
    6         16H2
    7         16H2
    8         16H2
    9         15H2
    ...etc...
</code></pre>

<p>So I thought I'd create a <code>function</code> and then use <code>map</code>.</p>

<pre><code>def is_in_range(x):
   if x['date'] &gt;= '01-01-16'  x['date'] &lt;= '06-31-16':
       print '16H1'
   elif x['date'] &gt;= '07-01-16' and x['date'] &lt;= '12-31-16':
       print '16H2'
   elif x['date'] &gt;= '01-01-15' and x['date'] &lt;= '06-31-15':
       print '15H1'
   elif x['date'] &gt;= '07-01-15' and x['date'] &lt;= '12-31-15':
       print '15H2'  
   ...etc...
</code></pre>

<p>I call the function like this:</p>

<pre><code>df.groupby(df['date'].map(is_in_range))
</code></pre>

<p>Now this gives me:</p>

<blockquote>
  <p>`TypeError: 'Timestamp' object has no attribute '<strong>getitem</strong>'</p>
</blockquote>

<p>to begin with. I'm not sure why, but either way there surely must be a better way?</p>