title: pandas groupby aggregate keep equal values
body: <p>I am trying to build an aggregator which simply returns a value if it is equal to all other values in the variable and NaN if it isn't.</p>
  
  <p>It is ment to keep meta information while aggregating sensory data. </p>
  
  <p>I get a strange key error... </p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  df = pd.DataFrame.from_dict({'v1' : [1,1,1,2,2,2],
                               'v2' : [1,2,3,4,5,6],
                               'v3' : [1,1,1,2,3,2],
                               'v4' : [2,2,2,3,3,3]})
  def keep_equal(x):
      if (x == x[0]).all(): return x[0]
      else: return np.NaN
  
  df =  df.groupby(df["v1"], as_index=False, observed =True).agg(keep_equal)
  </code></pre>
  
  <p>expected output would be:</p>
  
  <pre><code>     v1   v2  v3  v4
   0   1  NaN   1   2
   1   2  NaN   NaN 3
  </code></pre>
  
  <p>But I get a key error:</p>
  
  <pre><code>Traceback (most recent call last):
  
    File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  
    File "pandas\_libs\hashtable_class_helper.pxi", line 998, in pandas._libs.hashtable.Int64HashTable.get_item
  
  KeyError: 0
  </code></pre>