Home > Blockchain >  Pandas ValueError: Invalid fill value with a <class 'numpy.ndarray'>
Pandas ValueError: Invalid fill value with a <class 'numpy.ndarray'>

Time:07-13

I have a Pandas DataFrame with a series containing dicts.

import pandas as pd

df = pd.DataFrame({
                   'c': [np.nan, 'N', np.nan, 'N'],
                   'd': [{'c':'C'}, np.nan, {'c':'C'}, np.nan]
                 })

I am attempting to fillna with:

df.fillna(np.where(df['d'].str['c'] == "C", "Y", "N"), inplace=True)

Traceback:

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py in fillna(self, value, method, axis, inplace, limit, downcast)
   6501             elif isinstance(value, ABCDataFrame) and self.ndim == 2:
   6502 
-> 6503                 new_data = self.where(self.notna(), value)._mgr
   6504             else:
   6505                 raise ValueError(f"invalid fill value with a {type(value)}")

ValueError: invalid fill value with a <class 'numpy.ndarray'>

CodePudding user response:

From the doc

This value cannot be a list.

out = df.fillna({'c':(df['d'].str['c'] == "C").map({True:  "Y", False: "N"})})
Out[68]: 
   c           d
0  Y  {'c': 'C'}
1  N         NaN
2  Y  {'c': 'C'}
3  N         NaN

CodePudding user response:

Here's a way to do what I believe you're trying for without using fillna():

df.c[df.c.isna()] = np.where(df.d.str['c'] == 'C', 'Y', 'N')

Output:

   c           d
0  Y  {'c': 'C'}
1  N         NaN
2  Y  {'c': 'C'}
3  N         NaN
  • Related