相关疑难解决方法(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万
查看次数

Python pandas删除了SettingWithCopyWarning

所以我使用了一个空的数据帧

df=data[['ID','Matrix','Name','Country', 'Units']]
df['Value']=''
Run Code Online (Sandbox Code Playgroud)

我用这样的代码填充它,它找到包含'Good','Bad'值的字符串df.Matrix并用以下值填充它们sch[i]:

df.loc[df.Matrix.str.contains('Good'),'Value'] = sch[2]
df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6]
df.loc[df.Matrix.str.contains('Excellent'),'Value'] = sch[8]
Run Code Online (Sandbox Code Playgroud)

我遇到了一堆像这两个不同的错误:

C:\Python33\lib\site-packages\pandas\core\strings.py:184: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  " groups, use str.extract.", UserWarning)

C:\Users\0\Desktop\python\Sorter.py:57: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
  df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我正在使用压缩代码

pd.options.mode.chained_assignment = None
Run Code Online (Sandbox Code Playgroud)

如果我不压制错误消息,我将得到大约20个错误消息.是否有其他格式我可以更改数据,以便我不会收到错误消息?

我使用python 3和pandas 0.131如果它有帮助

python pattern-matching pandas

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