Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas group by and summing over integer and timedelta

Tags:

python

pandas

I'm trying to sum multiple columns after a group-by with heterogeneous types (integer, float & timedelta)

In [1]: import pandas

In [2]: df = pandas.DataFrame({'key': [1, 1, 2, 2], 'val1': range(4), 'val2': [pandas.Timedelta(seconds=i) for i in range(4)], 'val3': [0.1 * i for i in range(4)]})

In [3]: df
Out[3]: 
   key  val1     val2  val3
0    1     0 00:00:00   0.0
1    1     1 00:00:01   0.1
2    2     2 00:00:02   0.2
3    2     3 00:00:03   0.3

In this example, val1 is column of integer, val2 a column of timedeltas and v3 a column of float.

In [4]: df.groupby('key').sum()
Out[4]: 
     val1  val3
key            
1       1   0.1
2       5   0.5

After summing, the timedelta column has disappeared

In [5]: df.groupby('key')['val2'].sum()
Out[5]: 
key
1   00:00:01
2   00:00:05
Name: val2, dtype: timedelta64[ns]

Selecting only this column shows that it's summable

In [6]: df.groupby('key')['val2', 'val3'].sum()
Out[6]: 
     val3
key      
1     0.1
2     0.5

In [7]: df.groupby('key')['val2', 'val3'].sum()
Out[7]: 
     val3
key      
1     0.1
2     0.5

What am i missing?

like image 918
Guillaume Thomas Avatar asked Oct 20 '25 14:10

Guillaume Thomas


1 Answers

As mentioned in the documentation, you can specify which aggregation function you want per column and "force" a function for the val2 column:

import numpy as np
...
In [68]: df.groupby('key').agg({'val1': np.sum, 'val2': np.sum, 'val3': np.sum})
Out[68]: 
     val3     val2  val1
key                     
1     0.1 00:00:01     1
2     0.5 00:00:05     5
like image 198
Guillaume Thomas Avatar answered Oct 22 '25 03:10

Guillaume Thomas