Home > other >  Using a IF statement in a DataFrame and getting an error: The truth value of a Series is ambiguous
Using a IF statement in a DataFrame and getting an error: The truth value of a Series is ambiguous

Time:04-13

I have a data frame of stock prices and returns imported from yahoo finance as below.

Date price return
2019-01-01 54 0.05
2019-02-01 46 -0.14
2019-03-01 48 0.04

where date is the index and the return is numeric.

I am trying to create a new column with will = equal 1 if the return on the following day is positive and equal -1 if the following return is negative.

I have used

if df['return'].shift(-1) > 0:
  df['Indicator'] = 1
else 
  df['Indicator'] = -1

However, I get the afformentioned error. I have tried using .all() but this makes all of the indicator column equal to 1. even when the return on the following day is negative

The desired output would be

Date price return indicator
2019-01-01 54 0.05 -1
2019-02-01 46 -0.14 1
2019-03-01 48 0.04 1

The last 1 in the indicator column is assuming the return the following day, 2019-04-01 is positive.

Any advice?

Thank you

CodePudding user response:

Use the numpy where function. Its more effective and simple:

import numpy as np
df['Indicator'] = np.where(df['return'].shift(-1)>0,1,-1 )

CodePudding user response:

This would do I think:

df['Indicator'] = df['return'].shift(-1).apply(lambda x: 1 if x > 0 else -1)
  • Related