I want to be able to return more than a single columns from a pandas UDF (apply function). This works nicely, as long as the data frame is non empty!
In case it is empty it fails with: not enough values to unpack (expected 3, got 0). Is this to be considered as a bug in pandas? Or should the user be forced to manually check the length of the filtered data frame before executing the function? Or is there a better way not to run into this problem?
import pandas as pd
df = pd.DataFrame({'foo':[1,2,3], 'bar':[4,5,6]})
def my_function(x):
#print(x)
# some computation
# returns multiple values (tuple)
# simplified here
return 1,1,1
df = df[df.foo > 10]
df['r1'], df['r2'], df['r3'] = zip(*df.apply(my_function, axis=1))
df