I have:
import pandas as pd
import numpy as np
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice((1, 2, np.nan), (5, 5)))
s = pd.Series(range(5))
I want to add s
to df
and broadcast across rows. Usually I'd:
df.add(s)
And be done with it. However, I want to fill missing values with 0
. So I thought I'd:
df.add(s, fill_value=0)
But I got:
NotImplementedError: fill_value 0 not supported
0 1 2 3 4
0 1.0 1.0 2.0 3.0 4.0
1 2.0 3.0 2.0 4.0 4.0
2 1.0 1.0 3.0 4.0 4.0
3 1.0 1.0 2.0 4.0 6.0
4 1.0 3.0 4.0 3.0 5.0
The stack trace indicates that this parameter was never implemented:
.../pandas/core/frame.py in _combine_match_columns(self, other, func, level, fill_value)
3470 if fill_value is not None:
3471 raise NotImplementedError("fill_value %r not supported" %
-> 3472 fill_value)
3473
I could just fill the missing values before addition:
In [43]: df.fillna(0).add(s)
Out[43]:
0 1 2 3 4
0 1 1 2 3 4
1 2 3 2 4 4
2 1 1 3 4 4
3 1 1 2 4 6
4 1 3 4 3 5
I ran into this issue also. In my case it's because I was adding a series to a dataframe.
The fill_value=0
instruction works for me when adding a series to a series or adding a dataframe to a dataframe.
I just made a new dataframe with the series as its only column and now I can add them with fill_value=0
.
df1.add(df2, fill_value=0) # This works
series1.add(series2, fill_value=0) # This works
df.add(series, fill_value=0) # Throws error
df.add(pd.DataFrame(series), fill_value=0) # Works again
I get the same error with 0
and other numbers. You could alternatively use df.fillna(0)
:
df.fillna(0).add(s)
print df
>>>
0 1 2 3 4
0 1.0 1.0 2.0 3.0 4.0
1 2.0 3.0 2.0 4.0 4.0
2 1.0 1.0 3.0 4.0 4.0
3 1.0 1.0 2.0 4.0 6.0
4 1.0 3.0 4.0 3.0 5.0
Just use .fillna(0)
on the df
:
df.fillna(0).add(s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With