Still very new to python. I have refactored a piece of coe and extracted the following code to a new method. It works fine but in search of more pythonic ways I feel the IF clauses are maybe clumsy. Essentially the function may be passed either a str or a number as the variable 'second_option' As a result there is a subtle difference to the next line.
I tried a conditional assignment but it seemed to get upset over the data frame elements of this.
def create_crossover_data(self, indicator, second_option, market_data):
if type(second_option) == str:
market_data['position'] = market_data[indicator] > market_data[second_option]
else:
if type(second_option) == int or float:
market_data['position'] = market_data[indicator] > second_option
market_data['pre_position'] = market_data['position'].shift(1)
market_data.dropna(inplace=True) # dropping the NaN values
market_data['crossover'] = np.where(market_data['position'] == market_data['pre_position'], False, True)
market_data.drop(columns=['position', 'pre_position'])