提问人:chorink65 提问时间:10/25/2022 更新时间:10/25/2022 访问量:22
解析 Python 脚本中的 SettingWithCopyWarning
resolving a SettingWithCopyWarning in a Python script
问:
我是 Python 的新手,正在做一个项目,以使用 yfinance 示例了解更多信息。当我运行下面的脚本时,我收到 SettingWithCopy 警告。在阅读了此处和其他地方的几篇文章后,我无法解决该警告。指导非常感谢。
# 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
)
这是我收到的警告:
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
答: 暂无答案
评论