0

I have very weird case of a warning appearing quite randomly. EDIT: Please note that this question is not about how to deal with the warning but why it appears/disappears based on what is a single value in a data frame.

I have two files - test1.csv with:

A,B,C
0,-0.7071068,0

and - test2.csv with:

A,B,C
0,0,0

I get the warning

c:\libs\python36-32\lib\site-packages\ipykernel_launcher.py:2: 
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

when using the first file with the code (first file):

df = pd.read_csv("test1.csv")
df['B'][df['B'] > -1] = np.nan

but not with (same code, second file):

df = pd.read_csv("test2.csv")
df['B'][df['B'] > -1] = np.nan

In each case running the code results in suggesting the warning is wrong (the change goes through, so I'm not working with a copy):

   A    B   C
0  0  NaN   0

I'm on Windows 10 with Python 3.6.5 64-bit, Jupyter 4.4.0 and Pandas 0.23.3

Adam Streck
  • 340
  • 2
  • 15
  • So `df.loc[df['B'] > -1, 'B'] = np.nan` not working? – jezrael Oct 10 '18 at 08:09
  • 1
    It can be used to hide the warning, but it does not explain why: B = -0.7071068 => warning appears, B = 0 => warning does not appear. In other words, why does changing the column type from float to int affect the behavior? – Adam Streck Oct 10 '18 at 09:13

0 Answers0