user3556757
user3556757

Reputation: 3619

Unexpected SettingWithCopyWarning

I've been avoiding most SettingWithCopy warnings by using .loc[: (foo, bar)] construction.

But I don't know how to properly construct one case:

for sec in security_list:
     stochs.loc[:, (sec,'entry_signal')][stochs[sec, 'raw_signal'].shift(1) == stochs[sec, 'raw_signal']] = 0  

What I'm trying to do: in the stochs dataframe, add a new column, entry_signal. entry_signal is 1, 0, or -1. It's 0 if the raw_signal is unchanged from the previous raw_signal, otherwise it's the value of the raw_signal (1 or -1).

I guess pandas doesn't like the second indexing fragment, [stochs[sec, 'raw_signal'].shift(1) == stochs[sec, 'raw_signal']] because that spawns a copy.

Can anyone advise how to re-write this statement properly?

Thanks

Upvotes: 2

Views: 62

Answers (1)

EdChum
EdChum

Reputation: 394469

Change to:

stochs.loc[stochs[sec, 'raw_signal'].shift(1) == stochs[sec, 'raw_signal'], (sec,'entry_signal')] = 0 

what you did is called chained indexing as you were double subscripting so you should put the condition inside the brackets to loc

Upvotes: 2

Related Questions