I'm still running in errors when trying to write code where I use pipe/lambda on Series/Dataframes so I wondered if there is a way to debug my code to get better understanding. Just a simple example: I have a Series:
s = pd.Series([10,20,30,40,50])
If I want to add some number to each element I could use:
s.apply(lambda x : x 5)
#or
s.pipe(lambda x : x 5)
both works fine. But if I want to use conditional lambda:
s.apply(lambda x : x 5 if x>20 else 0) # works
s.pipe(lambda x : x 5 if x>20 else 0) # doesn't work
I get ValueError, the truth of a Series is ambiguous. If I wouldn't know why I get the error here I would need to know what x
is...
This is just an example, it's NOT about how to get the conditional adding done. I just want to know what is happening there and therefore I need to know what is x
while executing my code.
Here is my question. When working on more complex matters and running into errors, is there a way I can debug my code to get to know the type or value of the x
at any moment in the lambda while executing the code? For example a print statement? It would help to understand why I fail sometimes.
CodePudding user response:
This case:
The difference is that the x
in apply
is a single element of the Series, since the lambda function is applied to all elements one after the other. From the docs:
Parameters:
func, function: Function to apply to each column or row.
In the case of pipe
, the x
is the series itself, which is why the conditional lambda doesn't work (if x>20
doesn't make sense). This is evident from the pipe
docs:
Parameters:
func, function: Function to apply to the Series/DataFrame.
General debugging:
Define a function instead of using a lambda expression, then you can add print statements as you desire:
def my_func(x):
print(x)
return x 5 if x>20 else 0
s.pipe(my_func)