perform operation on list of arguments passed to a function

cmp

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
_______| ________________
balderman

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count expected arguments to be passed to a function in Python

Perform a string operation for every element in a Python list

Get a list/tuple/dict of the arguments passed to a function?

Formatting multiple arguments passed to a function in Java

Call function passed as an argument with arguments

Get arguments from list passed to function inside a function

R pattern to modify arguments passed to generic function

Count number of arguments passed to function

Modify arguments passed to a variadic function inside that function

Can function arguments be passed into a string?

custom reduce function implementation - why is this particular list of arguments passed?

Calculator function based on passed in arguments

Perform an operation on multiple tables in a list

Perform async operation on list in dart

Are there any issues with reassigning arguments as they're passed to a function

Perform Function With Arguments From String

Catching variables passed to function with no arguments

[function.implode]: Invalid arguments passed error

Getting the list of arguments passed to a method in unix

Pass additional arguments to the function passed in map()

Printing integers passed as arguments in function in C

Function using iterators to perform a object operation

The arguments are not passed into the function correctly

validate the number of arguments passed to method/function

Perform function and then also perform whatever is passed as prop, ReactJS

Is there a function to make user choose which operation to perform?

Read List excel in R & then perform operation (two)

How to write a Generalized function to perform OR operation in TypeScript?

Is there a function in snakemake to make the list of output dependent on the arguments passed into the shell command

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive