Python Forum
Setting with copy warning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting with copy warning
#1
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
Reply
#2
(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
Reply
#3
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.
Reply
#4
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
Reply
#5
(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?
Reply
#6
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?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020