Pandas question

Is there any specific reason for why we're using



df.loc[df.short_exit,'positions_short']= 0 



and not



df['positions_short'][df.short_exit] = 0

Hello Alex,

The first one is the right way to go about it. This is called loc indexing. ( df.loc[df.short_exit,'positions_short']= 0 )

The second one which is also called chained indexing (df['positions_short'][df.short_exit] = 0) will lead to the SettingWithCopyWarning Warning. You can try it out once.

The reason for the warning is how different both operations are.

1) df['positions_short'][df.short_exit] = 0 - this operation is actually two operations. One where we select the df['positions_short'] and then once this is selected we select the df.short_exit rows from this. Now, the issue is we are not sure that here after df['positions_short'] operation whether we are applying [df.short_exit] on the original df object or a copy of that object. So the changes might not reflect in some cases. Pandas does not give a garuntee of that. Hence it raises a warning.

2) On the other hand in df.loc[df.short_exit,'positions_short']= 0 the .loc function makes sure that we are modifying the original df object. Pandas gives a garuntee of that. 

You can read more about it here