请解释一下这些Pandas SettingWithCopyWarning [s]

时间:2017-07-18 19:25:13

标签: python pandas

有人可以解释为什么以下直线:

nbrs['d_inv'] = nbrs['d'].pow(-1)
...
nbrs[k] = nbrs[c] * nbrs['d_inv']

(k和c是键)每个都引发一个SettingWithCopyWarning:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

当我尝试在iPython中复制此行为时:

import pandas as pd
import numpy.random as rand

t = pd.DataFrame()
t['d'] = rand.uniform(0,1,5)
t['d_inv'] = t.pow(-1)
t['sum'] = t['d'] + t['d_inv']

没有提出警告。这里发生了什么?

[编辑1]我没有尝试做链式索引。我只是想填充一个空列。

[编辑2]

nbrs = pd.DataFrame(row_generator())其中每一行都是Series:

=====  =====  =====  =====
name = None
--------------------------
lat    lon    head   d
=====  =====  =====  =====
float  float  float  float
=====  =====  =====  =====

[edit 3] NOT A DUPLICATE

waring消息变得更不具体

使用nbrs.loc[:,'d_inv'] = nbrs['d'].pow(-1)加注

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item_labels[indexer[info_axis]]] = value

0 个答案:

没有答案