Example
import pandas as pd
import numpy as np
d = {'l': ['left', 'right', 'left', 'right', 'left', 'right'],
'r': ['right', 'left', 'right', 'left', 'right', 'left'],
'v': [-1, 1, -1, 1, -1, np.nan]}
df = pd.DataFrame(d)
Problem
When a grouped framework contains a value np.NaN, I want the grouped sum to NaNbe as indicated by the flag skipna=Falsefor pd.Series.sum, as well as pd.DataFrame.sumhowever this
In [235]: df.v.sum(skipna=False)
Out[235]: nan
However, this behavior is not reflected in pandas.DataFrame.groupbyobject
In [237]: df.groupby('l')['v'].sum()['right']
Out[237]: 2.0
and cannot be applied directly to a method np.sum
In [238]: df.groupby('l')['v'].apply(np.sum)['right']
Out[238]: 2.0
Bypass
I can get around this by doing
check_cols = ['v']
df['flag'] = df[check_cols].isnull().any(axis=1)
df.groupby('l')['v', 'flag'].apply(np.sum).apply(
lambda x: x if not x.flag else np.nan,
axis=1
)
but it is ugly. Is there a better way?