cmp
cmp

Reputation: 568

perform operation on list of arguments passed to a function

I have defined a simple function whose arguments include a list of column names from a dataframe. The intention of the function is

  1. accept a list or args (dataframe column names)
  2. locate columns and perform an operation on them
  3. pass these as columns to a new dataframe and return this object

Currently the function only returns the first item in the list successfully and does not loop over the whole list in the args.

I have tried simplifying the function back to copying the columns from one DF and appending these as columns to the new dataframe, however the loop still breaks after the first item.

def gen_signal(self, model_outputs: list, start_date: str, end_date: str, mean = 0):
    signal_df = pd.DataFrame()

    for model in model_outputs:

      model_preds_df = self.model_preds_df.set_index(self.model_preds_df['dates'])

      model_result = pd.Series(self.model_preds_df[model], 
                               index = model_preds_df.index)


      filter_by_date_params = (model_result.index > start_date) & \
      (model_result.index < end_date)

      results_data = self.model_preds_df[model][filter_by_date_params]

      #perform some logic that has been omitted here for brevity 
      signal_final = np.mean(results_data) 

      signal_df[model + '_signal' + '_{0}'.format(start_date)] = signal_final

      return signal_df 


# test function
per_start = '2002-01-01'
per_end = '2019-07-01'


# call function passing list of df columns to perform op over
gen_signal(['model_1', 'model_2', 'model_3'], per_start, per_end)


#function returns signal df but for only one item in the list.

dates  |  model_1_signal
_______| ________________

Upvotes: 0

Views: 74

Answers (1)

balderman
balderman

Reputation: 23815

Below is a quick demo of the bug you are facing: (As mentioned by emilanov your return statement is part of the loop and should be moved out)

lst = [1,2,3]

def calc_sum_with_bug(lst):
  s = 0
  for x in lst:
    s += x
    return s

def calc_sum(lst):
  s = 0
  for x in lst:
    s += x
  return s    

print(calc_sum_with_bug(lst))
print(calc_sum(lst))

output

1
6

Upvotes: 1

Related Questions