Input contains NaN, infinity or a value too large for dtype('float64')

I'm trying to implement SVC on NIfty data

but I keep getting this error



" Input contains NaN, infinity or a value too large for dtype('float64')."



This is the code m running



nf = pd.read_csv('C:/Users/Downloads/Nifty.csv')

nf.index = pd.to_datetime(nf.Date)

nf.dropna()

nf.head()



nf['Open-Close'] = nf.Open - nf.Close

nf['High-Low'] = nf.High - nf.Low



x = nf[['Open-Close','High-Low']]

x.head()



#tgt variable

y = np.where(nf['Close'].shift(-1)>nf.Close,1,0)



t = 0.7

t = int(tlen(nf))



#training data

x_train = x[:t]

y_train = y[:t]



#testing data

x_test = x[t:]

y_test = y[t:]



pd.DataFrame(x).fillna('bfill')

x.replace([np.inf, -np.inf], np.nan, inplace=True)

np.where(x.values >= np.finfo(np.float64).max)

x.dropna()

print(x)



#train & test accuracy

accuracy_train = accuracy_score(y_train, cls.predict(x_train))

accuracy_test = accuracy_score(y_test, cls.predict(x_test))



print('\nTrain Accuracy:{: .2f}%'.format(accuracy_train
100))

print('Test Accuracy:{: .2f}%'.format(accuracy_test*100))



at this point Im getting following error

 

 Input contains NaN, infinity or a value too large for dtype('float64').

Hi Karan,



​The code that you sent is incomplete. However, the error you are getting is because of the NaN values in the dataset.



Use this line of code where you are dropping NaN values.

 

nf.dropna(inplace=True)

Can also check this link for a similar kind of error.

If you still face the issue, please send the complete code along with the data file.

Thanks!

I got the same error message when using sklearn with pandas. My solution is to reset the index of my dataframe df before running any sklearn code:

df = df.reset_index()