I'm new to Python and working on a project to learn more using a yfinance example. When I run the script below, I get a SettingWithCopy warning. After reading several articles here and elsewhere, I'm not able to resolve the warning. Guidance much appreciated.
# Set the timeframe you are interested in viewing.
net_historical = net.history(start="2020-01-02", end="2022-10-21", interval="1d")
# Create a new DataFrame called signals, keeping only the 'Date' & 'Close' columns.
#print(signals_df)
signals_df = net_historical.drop(columns=['Open', 'High', 'Low', 'Volume','Dividends', 'Stock Splits'])
signals_df = signals_df.copy()
# Set the short window and long windows
short_window = 50
long_window = 100
# Generate the short and long moving averages (50 and 100 days, respectively)
signals_df['SMA50'] = signals_df['Close'].rolling(window=short_window).mean()
signals_df['SMA100'] = signals_df['Close'].rolling(window=long_window).mean()
signals_df['Signal'] = 0.0
signals_df['Symbol'] = line_strip
# Generate the trading signal 0 or 1,
# where 0 is when the SMA50 is under the SMA100, and
# where 1 is when the SMA50 is higher (or crosses over) the SMA100
signals_df['Signal'][short_window:] = np.where(
signals_df['SMA50'][short_window:] > signals_df['SMA100'][short_window:], 1.0, 0.0
)
Here's the warning I'm getting:
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
signals_df['Signal'][short_window:] = np.where(
/var/folders/xj/401632wd1qx7h6t0kqfjl98m0000gn/T/ipykernel_14492/214794330.py:59: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame