I have a function that needs to return multiple values:
def max_dd(ser):
...
compute i,j,dd
return i,j,dd
if I have code like this that calls this function passing in a series:
date1, date2, dd = df.rolling(window).apply(max_dd)
however, I get an error:
pandas.core.base.DataError: No numeric types to aggregate
If I return a single value from max_dd, everything is fine. How do I return multiple values from a function that has been "apply"?
Rolling apply can only produce single numeric values. There is no support for multiple returns or even nonnumeric returns (like something as simple as a string) from rolling apply. Any answer to this question will be a work around.
That said, a viable workaround is to take advantage of the fact that
rollingobjects are iterable (as ofpandas 1.1.0).What’s new in 1.1.0 (July 28, 2020)
Meaning that it is possible to take advantage of the faster grouping and indexing operations of the rolling function, but obtain more flexible behaviour with python:
Sample DataFrame and output:
df:result: