Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas DataFrame add fill_value NotImplementedError

Tags:

python

pandas

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

Question: How do I get the result I was anticipating?

     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
like image 547
piRSquared Avatar asked Jun 17 '16 18:06

piRSquared


4 Answers

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
like image 184
chrisaycock Avatar answered Oct 12 '22 10:10

chrisaycock


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
like image 24
Matt Seaton Avatar answered Oct 12 '22 09:10

Matt Seaton


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
like image 24
andrew Avatar answered Oct 12 '22 10:10

andrew


Just use .fillna(0) on the df:

df.fillna(0).add(s)
like image 37
Stefan Avatar answered Oct 12 '22 08:10

Stefan