title: Python pandas groupby agg function on datetime columns
body: <p>There is some bogus behaviour at <code>.agg()</code> function in pandas 0.17.1 and numpy 1.10.1. The problem can be found in some datatype conversion. Here is an example dataframe:</p>
  
  <pre><code>import datetime as dt
  
  df_data = [[1,1,2]
      , ['foo', 'bar', 'bar'], [1,2,3]
      , [dt.date(2015,1,5), dt.date(2015,1,3), dt.date(2015,1,15)]
      , [dt.datetime(2015,1,5,23,16,59), dt.datetime(2015,1,5,21,16,59)
      , dt.datetime(2015,1,5,20,16,59)]
      , [dt.timedelta(minutes=60), dt.timedelta(minutes=120), dt.timedelta(minutes=12)]]
  
  df_cols = ['gb', 'a_string', 'a_int', 'a_date', 'a_datetime', 'a_timedelta']
  
  df = pd.DataFrame({i:j for i,j in zip(df_cols, df_data)})
  
  print(df[df_cols])
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>   gb a_string  a_int      a_date          a_datetime  a_timedelta
  0   1      foo      1  2015-01-05 2015-01-05 23:16:59     01:00:00
  1   1      bar      2  2015-01-03 2015-01-05 21:16:59     02:00:00
  2   2      bar      3  2015-01-15 2015-01-05 20:16:59     00:12:00
  </code></pre>
  
  <p>I want to calculate for example <code>min()</code> and some other statistics. I can use several <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#groupby-object-attributes" rel="nofollow">groupby object attribute</a> to get my stats (let's see only <code>min()</code>):</p>
  
  <pre><code>df_g = df.groupby('gb').min()
  print(df_g[df_cols[1:]])
  </code></pre>
  
  <p>The result is correct:</p>
  
  <pre><code>   a_string  a_int      a_date          a_datetime  a_timedelta
  gb                                                             
  1       bar      1  2015-01-03 2015-01-05 21:16:59     01:00:00
  2       bar      3  2015-01-15 2015-01-05 20:16:59     00:12:00
  </code></pre>
  
  <p>Or I can use the <code>agg()</code> function, but with this, some datatype conversion error occurs at <code>timedelta</code> objects:</p>
  
  <pre><code>df_g_2 = df.groupby('gb').agg({i:np.min for i in df_cols[1:]})
  print(df_g_2[df_cols[1:]])
  </code></pre>
  
  <p>This is the result:</p>
  
  <pre><code>   a_string  a_int      a_date          a_datetime    a_timedelta
  gb                                                               
  1       bar      1  2015-01-03 2015-01-05 21:16:59  3600000000000
  2       bar      3  2015-01-15 2015-01-05 20:16:59   720000000000
  </code></pre>
  
  <p>The same error occurs at <code>datetime</code> objects if there are only <code>NaT</code> values present in a group. Let's see a new dataframe:</p>
  
  <pre><code>df_data = [[1,1,2]
      , ['foo', 'bar', 'bar'], [1,2,3]
      , [dt.date(2015,1,5), dt.date(2015,1,3), dt.date(2015,1,15)]
      , [pd.NaT, pd.NaT
      , dt.datetime(2015,1,5,20,16,59)]
      , [dt.timedelta(minutes=60), dt.timedelta(minutes=120), dt.timedelta(minutes=12)]]
  
  df_cols = ['gb', 'a_string', 'a_int', 'a_date', 'a_datetime', 'a_timedelta']
  
  df = pd.DataFrame({i:j for i,j in zip(df_cols, df_data)})
  
  print(df[df_cols])
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>   gb a_string  a_int      a_date          a_datetime  a_timedelta
  0   1      foo      1  2015-01-05                 NaT     01:00:00
  1   1      bar      2  2015-01-03                 NaT     02:00:00
  2   2      bar      3  2015-01-15 2015-01-05 20:16:59     00:12:00
  </code></pre>
  
  <p>The <code>min()</code> works perfectly:</p>
  
  <pre><code>   a_string  a_int      a_date          a_datetime  a_timedelta
  gb                                                             
  1       bar      1  2015-01-03                 NaT     01:00:00
  2       bar      3  2015-01-15 2015-01-05 20:16:59     00:12:00
  </code></pre>
  
  <p>But the <code>agg()</code> gives wrong values:</p>
  
  <pre><code>   a_string  a_int      a_date               a_datetime    a_timedelta
  gb                                                                    
  1       bar      1  2015-01-03                      nan  3600000000000
  2       bar      3  2015-01-15 1420489019000000000.0000   720000000000
  </code></pre>
  
  <p>Be careful &amp; have nice day!</p>