title: Get first and last Index of a Pandas DataFrame subset
body: <p>I do got some data within a pandas DataFrame looking like this.</p>
  
  <pre><code>df =
          A       B
  time                               
  0.1     10.0    1
  0.15    12.1    2
  0.19    4.0     2
  0.21    5.0     2
  0.22    6.0     2
  0.25    7.0     1
  0.3     8.1     1
  0.4     9.45    2
  0.5     3.0     1
  </code></pre>
  
  <p>Based on the following condition I look for a generic solution to find the first and last index of every subset.</p>
  
  <pre><code>cond = df.B == 2
  </code></pre>
  
  <p>So far I tried using the groupby concept but without the expected result.</p>
  
  <pre><code>df_1 = cond.reset_index()
  df_2 = df_1.groupby(df_1['B']).agg(['first','last']).reset_index()
  </code></pre>
  
  <p>This is the output I got.</p>
  
  <pre><code>      B       time          
                first    last
  0    False    0.1      0.5
  1    True     0.15     0.4
  </code></pre>
  
  <p>This is the output I like to get.</p>
  
  <pre><code>      B       time          
                first    last
  0    False    0.1      0.1
  1    True     0.15     0.22
  2    False    0.25     0.3
  3    True     0.4      0.4
  3    False    0.5      0.5
  </code></pre>
  
  <p>How can I accomplish this by a more or less generic approach? </p>