SettingWithCopyWarning on Guided Project Stock Market Predictions

Screen Link:

My Code:

test = sp[sp["Date"]>datetime(2013,1,1)]

training_columns=["5 Day Price Avg", "30 Day Price Avg", "365 Day Price Avg"]

reg = linear_model.LinearRegression()[training_columns], train[["Close"]])
predictions = reg.predict(test[training_columns])

test["Predictions"] = predictions```

What I expected to happen:

I expect to add a new column to the test data frame with the predicted close values.

What actually happened: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation:
test[“Predictions”] = predictions

<!--Enter other relevant details below: -->
1 Like

Yeah, this is something that still confuses me and I never got around to understand the problem clearly.

Does using copy help to get rid of the warning?

test = sp[sp["Date"]>datetime(2013,1,1)].copy()

If I recall correctly, you should still be able to run the code because it’s just a warning and not an error. Though, depending on the circumstances, ignoring that warning might or might not be a good idea.

This guide by Dataquest might be more helpful than me: