I want to perform a calculation on a Pandas Series. This calculation involves min
and max
. The calculation is used twice. In both cases it is the same calculation except for the min
or max
functions, which should be different.
I've created a function to perform this calculation:
def my_calc(my_series):
return my_series.rolling(...).max()
I don't know how to pass max
as a parameter of my_calc
.
my_calc(my_series).max()
and my_calc(my_series).min()
There's really no pretty way to do it.
def my_calc(my_series, func=max):
if not func in {'min', 'max'}:
raise ValueError('{} is not a valid method name!'.format(func))
return getattr(my_series.rolling(...), func)()
foo = my_calc(some_series, 'min')
bar = my_calc(some_series, 'max')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments