gpt4 book ai didi

python - 如何处理 Pandas 中的SettingWithCopyWarning

转载 作者:行者123 更新时间:2023-12-02 02:19:38 29 4
gpt4 key购买 nike

背景

我刚刚将我的 Pandas 从 0.11 升级到 0.13.0rc1。现在,该应用程序弹出了许多新的警告。其中之一是这样的:

E:\FinReporter\FM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE

我想知道这到底是什么意思?我需要改变什么吗?

如果我坚持使用 quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE,我该如何暂停警告?

发出警告的函数

def _decode_stock_quote(list_of_150_stk_str):
"""decode the webpage and return dataframe"""

from cStringIO import StringIO

str_of_all = "".join(list_of_150_stk_str)

quote_df = pd.read_csv(StringIO(str_of_all), sep=',', names=list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg')) #dtype={'A': object, 'B': object, 'C': np.float64}
quote_df.rename(columns={'A':'STK', 'B':'TOpen', 'C':'TPCLOSE', 'D':'TPrice', 'E':'THigh', 'F':'TLow', 'I':'TVol', 'J':'TAmt', 'e':'TDate', 'f':'TTime'}, inplace=True)
quote_df = quote_df.ix[:,[0,3,2,1,4,5,8,9,30,31]]
quote_df['TClose'] = quote_df['TPrice']
quote_df['RT'] = 100 * (quote_df['TPrice']/quote_df['TPCLOSE'] - 1)
quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
quote_df['TAmt'] = quote_df['TAmt']/TAMT_SCALE
quote_df['STK_ID'] = quote_df['STK'].str.slice(13,19)
quote_df['STK_Name'] = quote_df['STK'].str.slice(21,30)#.decode('gb2312')
quote_df['TDate'] = quote_df.TDate.map(lambda x: x[0:4]+x[5:7]+x[8:10])

return quote_df

更多警告消息

E:\FinReporter\FM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
E:\FinReporter\FM_EXT.py:450: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
quote_df['TAmt'] = quote_df['TAmt']/TAMT_SCALE
E:\FinReporter\FM_EXT.py:453: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
quote_df['TDate'] = quote_df.TDate.map(lambda x: x[0:4]+x[5:7]+x[8:10])

最佳答案

创建 SettingWithCopyWarning 是为了标记可能令人困惑的“链接”分配,如下所示,它并不总是按预期工作,特别是当第一个选择返回副本时。 [参见GH5390GH5597用于背景讨论。]

df[df['A'] > 2]['B'] = new_val  # new_val not set in df

该警告提供了重写建议,如下所示:

df.loc[df['A'] > 2, 'B'] = new_val

但是,这不适合您的用法,相当于:

df = df[df['A'] > 2]
df['B'] = new_val

虽然很明显您不关心写入使其返回到原始帧(因为您正在覆盖对它的引用),但不幸的是,这种模式无法与第一个链式分配示例区分开来。因此出现(误报)警告。 docs on indexing 中解决了误报的可能性。 ,如果您想进一步阅读。您可以通过以下分配安全地禁用此新警告。

import pandas as pd
pd.options.mode.chained_assignment = None # default='warn'
<小时/>

其他资源

关于python - 如何处理 Pandas 中的SettingWithCopyWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59762098/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com