TypeError: only integer scalar arrays can be converted to a scalar index when use Pandas Fillna

222 views Asked by At

Update : It seems due to .loc ,if i uses the original df from pd.read_excel, it is fine.

I have a dataframe with Dtypes as follows.

This is csv for the dataframe : CSV File

Date datetime64[ns] Amout float64 Currency object ID object

enter image description here enter image description here

I used the following code to replace NaT, NaN

a=np.datetime64('2000-01-01')
values={'Date':a,'Amount':0,'Currency':'0','ID':'0'}
df.fillna(value=values,inplace=True)

However, I got the error : TypeError: only integer scalar arrays can be converted to a scalar index.

I also tried to fillna with each column and I saw no error message but the Nan and Nat still remain unchanged.

a=np.datetime64('2000-01-01')
df[['Date']].fillna(a,inplace=True)
df[['Amount']].fillna(0,inplace=True)
df[['Currency']].fillna('0',inplace=True)
df[['ID']].fillna('0',inplace=True)

It seems very strange to me since I have use fillna successfully many times. Please give me an advice. Thank you so much.

1

There are 1 answers

2
jezrael On

I think there are strings NaN in Currency and Id columns, so use:

df = df.replace({'Currency': {'NaN': '0'}, 'ID':{'NaN': '0'}})