1

Based on the responses I received in Pandas SettingWithCopyWarning: I'm thoroughly confused and the clear explanation I found at Pandas - Get first row value of a given column, I thought I had all my SettingWithCopyWarning errors solved. Unfortunately, they are back (Python 3.8.5), and I'd appreciate your assistance. My dataframe df has a column 'SBPi_min_time' which I refer to as t_min

  t_min
'SBPi_min_time'

df.head()
   SBPi_max_time  SBPi_max  SBPi_min_time  SBPi_min  delta_p
0         52.257   119.626         55.903   111.256    8.370
1         59.513   118.580         60.562   114.395    4.185
2         62.632   119.626         63.650   112.999    6.627
3         65.721   121.021         67.279   114.395    6.626
4         69.344   120.672         72.414   113.348    7.324

If I now try to copy a value from one line of df to the previous line, I get the infamous SettingWithCopyWarning. I have tried 5 distinct approaches, and get the error in every single case. It's worth noting that the first approach is the one that is recommended in the posts that I have posted links to:

df.iloc[i, df.columns.get_loc('SBPi_min_time')] = df.iloc[i+1, df.columns.get_loc('SBPi_min_time')]
df.iloc[i, df.columns.get_loc(t_min)] = df.iloc[i+1, df.columns.get_loc(t_min)]
df.iloc[i, 3] = df.iloc[i+1, 3]
df[t_min].iloc[i] = df[t_min].iloc[i+1]
df[t_min][i] = df[t_min][i+1]

If there was a way to create a new object (in this case a float) from df[t_min][i+1], I could do so, and then set df[t_min][i] to it, but there doesn't seem to a way in which to do it:

df_copy = df.copy(deep = True)
df[t_min][i] = df_copy[t_min][i+1]

gives me the same error. What on earth am I doing wrong, and what's the fix?

Many thanks in advance

Thomas Philips

Thomas Philips
  • 935
  • 2
  • 11
  • 22
  • it's only a warning - I would ignore it – anon01 Sep 28 '20 at 06:24
  • I'd have been happy to, except that I constantly worry that something is wrong somewhere. Given how common the operation is, surely this must have a solution that works reliably is every time? – Thomas Philips Sep 28 '20 at 06:31
  • 1
    Is possible see your complete code? – jezrael Sep 28 '20 at 06:36
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Trenton McKinney Sep 28 '20 at 07:24
  • Also see [Real Python : SettingWithCopyWarning in Pandas: Views vs Copies](https://realpython.com/pandas-settingwithcopywarning/) & [pandas User Guide: Indexing and selecting data](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html) – Trenton McKinney Sep 28 '20 at 07:25

0 Answers0