0

I have mod_df data frame with various symbols and its corresponding prices. ['Lower Low']: is a column which is used to find the Lower Low price values, & I use the below code & it works fine.

mod_df['Lower Low'] = np.where((mod_df['Helper_L'] != mod_df['Helper_L'].shift(-1))
                      & (mod_df['Price'] < mod_df['Price'].shift(1))
                      & (mod_df['Price'] < mod_df['Price'].shift(-1)), 'Lower Low', '')

Since I have different Symbols in Symbols column, I'm trying to use groupby and Lambda function using the below code but I get the error:

TypeError: mod..() missing 1 required positional argument: 'y'

mod_df['Lower Low'] = mod_df.groupby('Symbol')[['Helper_L', 'Price']].transform(lambda x, y: (np.where((x != x.shift(-1)) & (y < y.shift(1)) & (y < y.shift(-1)), 'Lower Low', '')))

could anyone suggest how to fix this

Gopinathan
  • 27
  • 5

3 Answers3

0

The issue in your code is with the lambda function inside the transform() method. The lambda function in this case should only take a single argument, which represents the grouped data frame. You are trying to pass two arguments, x and y, but the transform() method only provides a single argument.

mod_df['Lower Low'] = mod_df.groupby('Symbol')[['Helper_L',
'Price']].transform(lambda df: np.where((df['Helper_L'] != 
 df['Helper_L'].shift(-1)) & (df['Price'] < df['Price'].shift(1)) & 
 (df['Price'] < df['Price'].shift(-1)), 'Lower Low', ''))
  • code throws error: KeyError: 'Helper_L'. This is the 'Helper_L' column that I already have. mod_df['Helper_L'] = mod_df.groupby('Symbol')['diff'].transform(lambda x: (x > 0).cumsum().add(1) - 1) – Gopinathan Jun 15 '23 at 12:45
  • mod_df['Lower Low'] = mod_df.groupby('Symbol')[['Helper_L', 'Price']].transform(lambda x: np.where((x['Helper_L'] != x['Helper_L'].shift(-1)) & (x['Price'] < x['Price'].shift(1)) & (x['Price'] < x['Price'].shift(-1)), 'Lower Low', '')) – Salman Bukhari Jun 17 '23 at 09:43
0

Because the argument passed to the function applied by transform is a dataframe, not a tuple of columns, try:

mod_df['Lower Low'] = mod_df.groupby('Symbol').transform(
    lambda _df: np.where(
        (_df['Helper_L'] != _df['Helper_L'].shift(-1)) & (_df['Price'] < _df['Price'].shift(1)) & (_df['Price'] < _df['Price'].shift(-1)),
         'Lower Low',
         ''
         )
   )
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • code throws error: KeyError: 'Helper_L'. This is the 'Helper_L' column that I already have. mod_df['Helper_L'] = mod_df.groupby('Symbol')['diff'].transform(lambda x: (x > 0).cumsum().add(1) - 1) – Gopinathan Jun 15 '23 at 12:45
0
 mod_df['Lower Low'] = mod_df.groupby('Symbol')[['Helper_L', 
'Price']].transform(lambda x: np.where((x['Helper_L'] != x['Helper_L'].shift(-1))
                                                                  & (x['Price'] < 
x['Price'].shift(1))
                                                                  & (x['Price'] < 
x['Price'].shift(-1)),
                                                                  'Lower Low', ''))
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 17 '23 at 21:33