title: Pandas groupby expanding optimization of syntax
body: <p>I am using the data from the example shown here:
  <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="nofollow noreferrer">http://pandas.pydata.org/pandas-docs/stable/groupby.html</a>.  Go to the subheading:  New syntax to window and resample operations</p>
  
  <p>At the command prompt, the new syntax works as shown in the pandas documentation.  But I want to add a new column with the expanded data to the existing dataframe, as would be done in a saved program.</p>
  
  <p>Before a syntax upgrade to the groupby expanding code, I was able to use the following single line code:</p>
  
  <pre><code>df = pd.DataFrame({'A': [1] * 10 + [5] * 10, 'B': np.arange(20)})
  df['Sum of B'] = df.groupby('A')['B'].transform(lambda x: pd.expanding_sum(x))
  </code></pre>
  
  <p>This gives the expected results, but also gives an 'expanding_sum is deprecated' message.  Expected results are:</p>
  
  <pre><code>    A   B  Sum of B
  0   1   0         0
  1   1   1         1
  2   1   2         3
  3   1   3         6
  4   1   4        10
  5   1   5        15
  6   1   6        21
  7   1   7        28
  8   1   8        36
  9   1   9        45
  10  5  10        10
  11  5  11        21
  12  5  12        33
  13  5  13        46
  14  5  14        60
  15  5  15        75
  16  5  16        91
  17  5  17       108
  18  5  18       126
  19  5  19       145
  </code></pre>
  
  <p>I want to use the new syntax to replace the deprecated syntax.  If I try the new syntax, I get the error message:</p>
  
  <pre><code>df['Sum of B'] = df.groupby('A').expanding().B.sum()
  
  TypeError: incompatible index of inserted column with frame index
  </code></pre>
  
  <p>I did some searching on here, and saw something that might have helped, but it gave me a different message:</p>
  
  <pre><code>df['Sum of B'] = df.groupby('A').expanding().B.sum().reset_index(level = 0)
  
  ValueError: Wrong number of items passed 2, placement implies 1
  </code></pre>
  
  <p>The only way I can get it to work is to assign the result to a temporary df, then merge the temporary df into the original df:</p>
  
  <pre><code>temp_df = df.groupby('A').expanding().B.sum().reset_index(level = 0).rename(columns = {'B' : 'Sum of B'})
  new_df = pd.merge(df, temp_df, on = 'A', left_index = True, right_index = True)
  print (new_df)
  </code></pre>
  
  <p>This code gives the expected results as shown above.</p>
  
  <p>I've tried different variations using transform as well, but have not been able to come up with coding this in one line as I did before the deprecation.  Is there a single line syntax that will work?  Thanks.</p>