Reputation: 4426
Not sure why I still get the warning after I updated to .loc method suggested by the message? Is it a false alarm?
eG.loc[:,'wt']=eG.groupby(['date','BB'])['m'].transform(weightFunction)
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':
Upvotes: 4
Views: 6000
Reputation: 59
This is to avoid what is called chained indexing. Pandas Chained Index
As Marx suggested, by using the deep copy, you easily can skip this warning.
cp = df[df.a > 0].copy()
or if it is a heavy data set and you do not need the original one just replace the slice with the original.
df = df[df.a > 0]
Upvotes: 0
Reputation: 210852
I guess your eG
DF is a copy of another DF...
Here is a small demo:
In [69]: df = pd.DataFrame(np.random.randint(0, 5, (10, 3)), columns=list('abc'))
In [70]: cp = df[df.a > 0]
In [71]: cp.loc[:, 'c'] = cp.groupby('a').b.transform('sum')
c:\envs\py35\lib\site-packages\pandas\core\indexing.py:549: 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
Workaround:
In [72]: cp = df[df.a > 0].copy()
In [73]: cp.loc[:, 'c'] = cp.groupby('a').b.transform('sum')
Or if you don't need original DF you can save memory:
In [74]: df = df[df.a > 0]
In [75]: df.loc[:, 'c'] = df.groupby('a').b.transform('sum')
Upvotes: 15