0

I'm trying to plot a stock prediction model. Everything is working fine until I try to index and plot.

valid['Predictions'] = 0
valid['Predictions'] = preds
valid.index = new_data[valid_nbr:].index
train.index = new_data[:train_nbr].index
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])

This is the error message I get after it runs:

/usr/lib/python3/dist-packages/ipykernel_launcher.py:2: 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: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

/usr/lib/python3/dist-packages/ipykernel_launcher.py:3: 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: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  This is separate from the ipykernel package so we can avoid doing imports until

How do I convert my code to follow the documentation the error provides? Thanks.

Jxck
  • 51
  • 1
  • 6
  • https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas It's a false positive, you probably don't want to change your code. Try ```pd.options.mode.chained_assignment = None```. – nathan.j.mcdougall Sep 12 '19 at 23:11
  • ^^ No I wouldn't do that. You should try to understand the error. When you don't have chained assignment and this error turns up, it's often because you susbet a DataFrame in previous steps, which returns a view of the original, not its own object. This often happens in steps like `df = df[some_mask]` or `df = df.drop_duplicates()`. Neither of those create new objects, so later steps throw the warning. Add a `.copy()` on the end of your code to ensure you get a new object, then when you do `valid['predictions'] = 0` the error will disappear – ALollz Sep 12 '19 at 23:42

1 Answers1

1

Your code implies you have three dataframes you're working with: valid, new_data and train. The warning indicates that you don't actually have three dataframes, but one or more of those three is actually just a subset of the other. When you go to change the index of valid or train, it can't tell whether you're wanting to change the index on your whole dataframe, or just the subset of your dataframe.

You can get around this by subsetting your dataframe using the .loc notation.

Alternatively, if you create a dataframe using indexing you can put .copy() at the end to unambiguously create a new dataframe object. e.g. df2 = df1[df1['foo']=='a'].copy()

KWx
  • 310
  • 1
  • 10