0

Having read this answer, I tried to do the following to avoid SettingWithCopyWarning.

So I did below. Yet it still generates the warning below. What have I done wrong ?

df_filtered.loc[:,'MY_DT'] = pd.to_datetime(df_filtered['MY_DT'])
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

My column was originally a string

df_filtered['MY_DT']
Out[3]: 
0       4/24/2020
1       4/24/2020
2       4/24/2020
3       4/24/2020
10      4/24/2020
          ...    
1937    4/30/2020
1938    4/30/2020
1939    4/30/2020
1940    4/30/2020
1941    4/30/2020
Name: MY_DT, Length: 1896, dtype: object
3
  • Presumably df_filtered is a slice of another DataFrame? You don't show enough of your code for us to be certain. Commented Apr 2, 2020 at 9:06
  • Yes it is a slice of another dataframe. Commented Apr 2, 2020 at 9:30
  • Then that's the problem - you need to make a copy of the slice before you perform any assignments - see theletz's answer. Commented Apr 2, 2020 at 9:38

1 Answer 1

1

Probably df_filtered is a sub dataframe of other one (df?).

This warning means that you try to change df_filtered which is a slice of df, and it will not change df.

In order to avoid this warning you can try to copy the slice:

df_filtered = df_filtered.copy()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.