Jojo
Jojo

Reputation: 1117

TyperError when converting NaN's into number in DataFrame

I have a DataFrame df that looks as follows and I'm trying to convert all cases where there is a NaN in the closing_price column into 100.

   maturity_dt              pay_freq_cd   coupon closing_price FACE_VALUE  
0   2017-06-30 00:00:00.0           2    0.625      99.96875        100   
1   2015-07-15 00:00:00.0           2      1.6         99.47        100   
2   2018-06-15 00:00:00.0           2    1.125      100.3906        100   
3   2015-07-13 00:00:00.0           2      2.1           NaN        100 

I try to do that with the code below, but I get the error TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' in the line price_array = np.where(np.isnan(price_array), 100, price_array).

price_array = df['closing_price'].values
price_array = np.where(np.isnan(price_array), 100, price_array)

Upvotes: 1

Views: 67

Answers (1)

Alex Riley
Alex Riley

Reputation: 176820

Use Pandas' fillna() method instead. In your case you can write:

df['closing_price'].fillna(100)

This replaces the NaN values in the 'closing_price' column with the value 100. Different data types are handled correctly by Pandas. (Remember to assign the new column back to the DataFrame or use inplace=True.)


The error you see is a result of the 'closing_price' column having the object data type. np.isnan expects an array of float values. To get around this, you can cast the column to floating type with

df['closing_price'] = df['closing_price'].astype(float)

...and then use your method as normal (although I'd still favour fillna()).

Upvotes: 2

Related Questions