0

I am getting the SettingWithCopyWarning on the following code:

combined_updated['institute_service'] = combined_updated['institute_service'].copy().astype('float')

Could someone in the community help me out in understanding why I keep getting that warning?

DaveL17
  • 1,673
  • 7
  • 24
  • 38
  • does this help: https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas ? – TheHappyBee Jul 19 '21 at 20:31
  • Thanks. I saw this article initially and it helped me fix the warning with indexing for the following code: ```combined_updated.loc[combined_updated['institute_service']=='Less than 1 year','institute_service']=combined_updated['institute_service'][combined_updated['institute_service']=='Less than 1 year'].str.replace('Less than 1 year', '1.0')``` . The problem now that I keep getting the same warning on the code that I put in the initial question. – Zadik Lopez Jul 19 '21 at 21:39

1 Answers1

0

There's no need to use copy() to change the column to float; maybe the .copy() is giving you the warning. You can also try resetting the index before you do the operation, that usually works for me when I get that warning.

combined_updated = combined_updated.reset_index(drop=True)
combined_updated['institute_service'] = combined_updated['institute_service'].astype(float)
Hammurabi
  • 1,141
  • 1
  • 4
  • 7
  • Thank you @Hammurabi. Still getting the same warning but with a different message: ```self.obj[item] = s``` . Any additional help would be deeply appreciated. – Zadik Lopez Jul 20 '21 at 16:47
  • Can you update your post with what self.obj[item] is? I'm not clear on how you are going from combined_updated to self.obj[item]. – Hammurabi Jul 20 '21 at 19:10
  • Hello @Hammurabi, This is the cell that is giving me the warning message: ```combined_updated.loc[combined_updated['institute_service']=='7-10','institute_service']=combined_updated['institute_service'][combined_updated['institute_service']=='7-10'].str.replace('7-10', '10.0') combined_updated = combined_updated.reset_index(drop = True) combined_updated['institute_service'] = combined_updated['institute_service'].astype(float)``` – Zadik Lopez Jul 20 '21 at 21:40
  • Sorry, I can't tell why you are getting the warning, I can't replicate that error. Also, I don't understand how you can get from that to self.obj[item] == s. Maybe try just .head(5) and see if you get the error on the first 5 rows, then the first 10, etc.? Maybe something unexpected is in your df. – Hammurabi Jul 20 '21 at 21:59