title: Computing np.diff in Pandas after using groupby leads to unexpected result
body: <p>I've got a dataframe, and I'm trying to append a column of sequential differences to it.  I have found a method that I like a lot (and generalizes well for my use case).  But I noticed one weird thing along the way.  Can you help me make sense of it?</p>
  
  <p>Here is some data that has the right structure (code modeled on an answer <a href="https://stackoverflow.com/a/20649094/2501018">here</a>):</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  import random
  from itertools import product
  
  random.seed(1)       # so you can play along at home
  np.random.seed(2)    # ditto
  
  # make a list of dates for a few periods
  dates = pd.date_range(start='2013-10-01', periods=4).to_native_types()
  # make a list of tickers
  tickers = ['ticker_%d' % i for i in range(3)]
  # make a list of all the possible (date, ticker) tuples
  pairs = list(product(dates, tickers))
  # put them in a random order
  random.shuffle(pairs)
  # exclude a few possible pairs
  pairs = pairs[:-3]
  # make some data for all of our selected (date, ticker) tuples
  values = np.random.rand(len(pairs))
  
  mydates, mytickers = zip(*pairs)
  data = pd.DataFrame({'date': mydates, 'ticker': mytickers, 'value':values})
  </code></pre>
  
  <p>Ok, great.  This gives me a frame like so:</p>
  
  <pre><code>     date        ticker      value
  0    2013-10-03  ticker_2    0.435995
  1    2013-10-04  ticker_2    0.025926
  2    2013-10-02  ticker_1    0.549662
  3    2013-10-01  ticker_0    0.435322
  4    2013-10-02  ticker_2    0.420368
  5    2013-10-03  ticker_0    0.330335
  6    2013-10-04  ticker_1    0.204649
  7    2013-10-02  ticker_0    0.619271
  8    2013-10-01  ticker_2    0.299655
  </code></pre>
  
  <p>My goal is to add a new column to this dataframe that will contain sequential changes.  The data needs to be in order to do this, but the ordering and the differencing needs to be done "ticker-wise" so that gaps in another ticker don't cause NA's for a given ticker. I want to do this without perturbing the dataframe in any other way (i.e. I do not want the resulting DataFrame to be reordered based on what was necessary to do the differencing).  The following code works:</p>
  
  <pre><code>data1 = data.copy() #let's leave the original data alone for later experiments
  data1.sort(['ticker', 'date'], inplace=True)
  data1['diffs'] = data1.groupby(['ticker'])['value'].transform(lambda x: x.diff())
  data1.sort_index(inplace=True)
  data1
  </code></pre>
  
  <p>and returns:</p>
  
  <pre><code>     date        ticker      value       diffs
  0    2013-10-03  ticker_2    0.435995    0.015627
  1    2013-10-04  ticker_2    0.025926   -0.410069
  2    2013-10-02  ticker_1    0.549662    NaN
  3    2013-10-01  ticker_0    0.435322    NaN
  4    2013-10-02  ticker_2    0.420368    0.120713
  5    2013-10-03  ticker_0    0.330335   -0.288936
  6    2013-10-04  ticker_1    0.204649   -0.345014
  7    2013-10-02  ticker_0    0.619271    0.183949
  8    2013-10-01  ticker_2    0.299655    NaN
  </code></pre>
  
  <p>So far, so good.  If I replace the middle line above with the more concise code shown here, everything still works:</p>
  
  <pre><code>data2 = data.copy()
  data2.sort(['ticker', 'date'], inplace=True)
  data2['diffs'] = data2.groupby('ticker')['value'].diff()
  data2.sort_index(inplace=True)
  data2
  </code></pre>
  
  <p>A quick check shows that, in fact, <code>data1</code> is equal to <code>data2</code>.  However, if I do this:</p>
  
  <pre><code>data3 = data.copy()
  data3.sort(['ticker', 'date'], inplace=True)
  data3['diffs'] = data3.groupby('ticker')['value'].transform(np.diff)
  data3.sort_index(inplace=True)
  data3
  </code></pre>
  
  <p>I get a strange result:</p>
  
  <pre><code>     date        ticker     value       diffs
  0    2013-10-03  ticker_2    0.435995    0
  1    2013-10-04  ticker_2    0.025926   NaN
  2    2013-10-02  ticker_1    0.549662   NaN
  3    2013-10-01  ticker_0    0.435322   NaN
  4    2013-10-02  ticker_2    0.420368   NaN
  5    2013-10-03  ticker_0    0.330335    0
  6    2013-10-04  ticker_1    0.204649   NaN
  7    2013-10-02  ticker_0    0.619271   NaN
  8    2013-10-01  ticker_2    0.299655    0
  </code></pre>
  
  <p>What's going on here?  When you call the <code>.diff</code> method on a Pandas object, is it not just calling <code>np.diff</code>?  I know there's a <code>diff</code> method on the <code>DataFrame</code> class, but I couldn't figure out how to pass that to <code>transform</code> without the <code>lambda</code> function syntax I used to make <code>data1</code> work.  Am I missing something?  Why is the <code>diffs</code> column in <code>data3</code> screwy?  How can I have call the Pandas <code>diff</code> method within <code>transform</code> without needing to write a <code>lambda</code> to do it?</p>