简体   繁体   中英

Pandas: nansum Series to each column of DataFrame

I would like to sum a Series to each of the columns of a DataFrame, having the same behaviour of nansum: if one component is missing, return the other; if both are missing return NaN/0.

Test case:

import numpy as np
import pandas as pd

df = pd.DataFrame([[np.nan, 1], [2, 2], [3, 4]])
ts = pd.Series([1, np.nan, 4])

Attempt 1 (gives nan when one is missing):

df.add(ts, axis=0)

Out[115]: 
     0    1
0  NaN  2.0
1  NaN  NaN
2  7.0  8.0

Attempt 2 (throws error):

df.add(ts, axis=0, fill_value=0)
NotImplementedError: fill_value 0 not supported.

Expected Output:

   0  1
0  1  2
1  2  2
2  7  8

Note: I could do it by saving location of NaNs, doing the sum with NaN filled with 0 and set to NaN when both are NaN, but I am looking for a better solution.

fill_value for anything besides None is still in the works, but as of now, you'll have to workaround this. Perform addition, and then compute the mask to reinstate NaN s where required.

r = df.fillna(0).add(ts.fillna(0), axis=0)

m = df.isnull().__and__(ts.isnull(), axis=0).values
v = r.values
v[m] = np.nan

r[:] = v 
r
     0    1
0  1.0  2.0
1  2.0  2.0
2  7.0  8.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM