Python error help: "ValueError: Input contains NaN, infinity or a value too large for dtype('float64')."

Viewed 314

I get the error when trying to run the following code:

df = pd.DataFrame(pd.read_csv('x_y.csv'))
pd.set_option('display.max_columns', None)
df = df.reset_index()
X = df['EU_Sales']
X.fillna(X.mean())
X = X.to_numpy().reshape(-1, 1)
np.nan_to_num(X)
df = df.reset_index()
est = KBinsDiscretizer(n_bins=2, encode='ordinal', strategy='quantile')
Xt = est.fit_transform(X)

I am trying to read from a column in my training set, I then turn it into an array with the reshape function, I printed the array to make sure this function works and it does. But for some reason when trying to use the KBinsDiscretizer function, I get an error code saying I have a null or infinity value in my array.

What is the problem?

1 Answers

You did not assign X after filling NaN. So your X still has NaN.

#...
X.fillna(X.mean()) # fills NaN but change is not permanent

Since inplace is False by default the operation is performed and returns a copy of the object. You then need to assign it to a variable or set inplace=True

X = X.fillna(X.mean()) # fills NaN and change assign to X 

Or

X.fillna(X.mean(), inplace=True)
Related