相关疑难解决方法(0)

如何在Pandas中处理SettingWithCopyWarning?

背景

我刚刚将我的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
Run Code Online (Sandbox Code Playgroud)

我想知道究竟是什么意思?我需要改变什么吗?

如果我坚持使用,我应该如何暂停警告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'] …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas chained-assignment

536
推荐指数
16
解决办法
58万
查看次数

熊猫:SettingWithCopyWarning

我想用Pandas DataFrame大于任意数字(在这种情况下NaN为100)的值替换(因为这个值很大,表示实验失败).以前我用它来替换不需要的值:

sve2_all[sve2_all[' Hgtot ng/l'] > 100] = np.nan
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

-c:3: 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
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\indexing.py:346: 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
self.obj[item] = s
Run Code Online (Sandbox Code Playgroud)

这个StackExchange问​​题来看,似乎有时候这个警告可以被忽略,但我不能很好地跟进讨论,以确定这是否适用于我的情况.这个警告基本上让我知道我会覆盖我的一些价值观DataFrame吗?

编辑:据我所知,一切都表现得如此.跟进是我取代非标准价值的方法吗?有没有更好的方法来取代价值观?

python python-2.7 pandas chained-assignment

18
推荐指数
1
解决办法
3万
查看次数