Reputation:
According to this strategy, we should invest between November and April and don't invest between May and October. Add a column month
to the DataFrame world_returns
with the calendar month of each observation. For every row, in this column, you should have a number from 1 to 12 indicating to which calendar month that observation belongs. Add a second column called signal
that is equal to 1
if the month is between November and April (included) and 0
otherwise.
Can someone please help with the second part of the question for the column 'signal'
As of now i have put:
world_returns['month'] = world_returns.index.month
world_returns['signal'] = np.where(world_returns['month'] ==(11,12,1,2,3,4) ,1 ,0)
world_returns.head()
Upvotes: 0
Views: 44
Reputation: 3300
The issue with what you have tried is you are trying to check where the month column contains the whole tuple (11,12,1,2,3,4)
, rather than checking where the values in month are in that tuple. Your code would work if your dataframe looked like below:
data month
0 foo 1
1 foo 2
2 foo (11, 12, 1, 2, 3, 4)
Then np.where(df['month'] == (11,12,1,2,3,4) ,1 ,0)
returns array([0, 0, 1])
.
What you want to do is have a 1 if the month is in (11,12,1,2,3,4). To do this, you need to edit your conditional statement within the where clause. The function .isin()
can be used to check if a value is contained within a list, like the below:
world_returns['month'].isin([11,12,1,2,3,4])
If you change your conditional statement in the where clause, you should get the desired result.
Upvotes: 1