Python Forum
Setting with copy warning - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Setting with copy warning (/thread-39407.html)



Setting with copy warning - catlessness - Feb-13-2023

Why am I getting the "setting with copy" warning with the code below? Am I not doing what they suggested? And the result is wrong so I guess there is some problem with this.
import pandas as pd
'''
...
'''
#dfout is a pd.DataFrame instance
dfout.columns=[0,1,2]
dfout.loc[:,3]=dfout.loc[:,0]
dfout.loc[:,4]=dfout.loc[:,1]
dfout.loc[:,5]=dfout.loc[:,2]
dfout.loc[:,6]=np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7]=np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360
Thanks


RE: Setting with copy warning - catlessness - Feb-13-2023

(Feb-13-2023, 02:07 PM)catlessness Wrote: Why am I getting the "setting with copy" warning with the code below? Am I not doing what they suggested? And the result is wrong so I guess there is some problem with this.
import pandas as pd
'''
...
'''
#dfout is a pd.DataFrame instance
dfout.columns=[0,1,2]
dfout.loc[:,3]=dfout.loc[:,0]
dfout.loc[:,4]=dfout.loc[:,1]
dfout.loc[:,5]=dfout.loc[:,2]
dfout.loc[:,6]=np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7]=np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360
Thanks
Oh the result is right I just checked again


RE: Setting with copy warning - Vadanane - Feb-13-2023

If you are looking for help with a specific problem, it is best to post a new thread with a detailed description of your issue. This will help other members of the community provide you with the best advice and assistance.


RE: Setting with copy warning - Vadanane - Feb-14-2023

You are getting the "setting with copy" warning because you are trying to assign a value to a subset of a DataFrame without using the .loc accessor. The .loc accessor is used to ensure that the original DataFrame is not modified in place.

To fix this, you should use the .loc accessor when assigning values to a subset of a DataFrame:

dfout.loc[:,3] = dfout.loc[:,0]
dfout.loc[:,4] = dfout.loc[:,1]
dfout.loc[:,5] = dfout.loc[:,2]
dfout.loc[:,6] = np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7] = np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360


RE: Setting with copy warning - catlessness - Feb-14-2023

(Feb-14-2023, 09:59 AM)Vadanane Wrote: You are getting the "setting with copy" warning because you are trying to assign a value to a subset of a DataFrame without using the .loc accessor. The .loc accessor is used to ensure that the original DataFrame is not modified in place.

To fix this, you should use the .loc accessor when assigning values to a subset of a DataFrame:

dfout.loc[:,3] = dfout.loc[:,0]
dfout.loc[:,4] = dfout.loc[:,1]
dfout.loc[:,5] = dfout.loc[:,2]
dfout.loc[:,6] = np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7] = np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360

Thank you, but isn't this exactly what I have done?


RE: Setting with copy warning - deanhystad - Feb-21-2023

The warning is triggered by a pattern, not an actual promlem. Sometimes you cannot do anything to stop the warning other than not modifying slices in the dataframe.

Why are you using slices instead of indicies?