title: Why does pandas groupby().transform() require a unique index?
body: <p>I want to use groupby().transform() to do a custom (cumulative) transform of each block of records in a (sorted) dataset.  Unless I ensure I have a unique key, it doesn't work.  Why?</p>
  
  <p>Here's a toy example:</p>
  
  <pre><code>df = pd.DataFrame([[1,1],
                    [1,2],
                    [2,3],
                    [3,4],
                    [3,5]], 
                    columns='a b'.split())
  df['partials'] = df.groupby('a')['b'].transform(np.cumsum)
  df
  </code></pre>
  
  <p>gives the expected:</p>
  
  <pre><code>     a   b   partials
  0    1   1   1
  1    1   2   3
  2    2   3   3
  3    3   4   4
  4    3   5   9
  </code></pre>
  
  <p>but if 'a' is a key, it all goes wrong:</p>
  
  <pre><code>df = df.set_index('a')
  df['partials'] = df.groupby(level=0)['b'].transform(np.cumsum)
  df
  
  ---------------------------------------------------------------------------
  Exception                                 Traceback (most recent call last)
  &lt;ipython-input-146-d0c35a4ba053&gt; in &lt;module&gt;()
        3 
        4 df = df.set_index('a')
  ----&gt; 5 df.groupby(level=0)['b'].transform(np.cumsum)
  
  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/groupby.pyc in transform(self, func, *args, **kwargs)
     1542             res = wrapper(group)
     1543             # result[group.index] = res
  -&gt; 1544             indexer = self.obj.index.get_indexer(group.index)
     1545             np.put(result, indexer, res)
     1546 
  
  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/index.pyc in get_indexer(self, target, method, limit)
      847 
      848         if not self.is_unique:
  --&gt; 849             raise Exception('Reindexing only valid with uniquely valued Index '
      850                             'objects')
      851 
  
  Exception: Reindexing only valid with uniquely valued Index objects
  </code></pre>
  
  <p>Same error if you select column 'b' before grouping, ie.</p>
  
  <pre><code>df['b'].groupby(level=0).transform(np.cumsum)
  </code></pre>
  
  <p>but you can make it work if you transform the entire dataframe, like:</p>
  
  <pre><code>df.groupby(level=0).transform(np.cumsum)
  </code></pre>
  
  <p>or even a one-column dataframe (rather than series):</p>
  
  <pre><code>df.groupby(level=0)[['b']].transform(np.cumsum)
  </code></pre>
  
  <p>I feel like there's some still some deep part of <a href="http://wesmckinney.com/blog/?p=125" rel="noreferrer">GroupBy-fu</a> that I'm missing.  Can someone set me straight?</p>