title: Grouping and Transforming Pandas Data
body: <p>I have a time series data set whith 10 units of oberservation. On each day each unit of observation may engage in an activity for a number of hours. I have therefore (lets say) 4 variables in a DataFrame:</p>
  
  <p>Date - date
  Unit - unit of observation
  acitvity - some activity
  CumHours - the cumulative number of hours spent by a unit on an activity.</p>
  
  <p>I need to create an 'Hours_calc' variable that says how many hours were spent on any activity by each line on each day. After importing all the normal bits an pieces I do this:</p>
  
  <pre><code>DF = (mydata, columns = ['Date', 'Unit', 'Activity', 'CumHours'])
  DF['Hours_calc'] = np.nan
  DFGrouped = DF.groupby(['Unit', 'Activity'])
  </code></pre>
  
  <p>I believe I need the transform method, and I think that the first argument passed to the transform function (as specified) is the DF column as a series as with the agg() method. Is that right? </p>
  
  <p>I wrote the following function, but as you will see I have no idea whether for the calculation I should be referring to DF or DFGrouped, or what? In general how to refer to rows/columns within goruped data? Also, am I right to try to return a series the same length as the groupby group?</p>
  
  <pre><code>def WorkingHours(x, Column):
      LISTHOURS = []
      for row in xrange(1, len(x)):
          Tot = int(DFGrouped[Column][row]) - int(DFGrouped[Column][row - 1])
          LISTHOURS.append(Tot)
      return pd.Series(LISTHOURS)
  
  TESTDF = DFGrouped.transform({Hours_calc : lambda x : WorkingHours(x, 'CumHours')})
  </code></pre>
  
  <p>Also I am not seeing anything in the documentation about passing a Dict to transform, but I can't see any other way of performing the function on only one variable/column in the gorup. </p>
  
  <p>Where Am I going wrong? The error message is long an confusing but ends in "Transform function invalid for data types"</p>