Correct way to set new column in pandas DataFrame to avoid SettingWithCopyWarning

As it says in the error, try using .loc[row_indexer,col_indexer] to create the new column.

netc.loc[:,"DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM.

Notes

By the Pandas Indexing Docs your code should work.

netc["DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

gets translated to

netc.__setitem__('DeltaAMPP', netc.LOAD_AM - netc.VPP12_AM)

Which should have predictable behaviour. The SettingWithCopyWarning is only there to warn users of unexpected behaviour during chained assignment (which is not what you're doing). However, as mentioned in the docs,

Sometimes a SettingWithCopy warning will arise at times when there’s no obvious chained indexing going on. These are the bugs that SettingWithCopy is designed to catch! Pandas is probably trying to warn you that you’ve done this:

The docs then go on to give an example of when one might get that error even when it's not expected. So I can't tell why that's happening without more context.


Your example is incomplete, as it doesn't show where netc comes from. It is likely that netc itself is the product of slicing, and as such Pandas cannot make guarantees that it isn't a view or a copy.

For example, if you're doing this:

netc = netb[netb["DeltaAMPP"] == 0]
netc["DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

then Pandas wouldn't know if netc is a view or a copy. If it were a one-liner, it would effectively be like this:

netb[netb["DeltaAMPP"] == 0]["DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

where you can see the double indexing more clearly.

If you want to make netc separate from netb, one possible remedy might be to force a copy in the first line (the loc is to make sure we're not copying two times), like:

netc = netb.loc[netb["DeltaAMPP"] == 0].copy()

If, on the other hand, you want to have netb modified with the new column, you may do:

netb.loc[netb["DeltaAMPP"] == 0, "DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

You need to reset_index when you will create column especially if you have filtered on specific values... then you don't need to use .loc[row_indexer,col_indexer]

netc.reset_index(drop=True, inplace=True)
netc["DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

Then it should work :)

Tags:

Python

Pandas