我有一个函数,根据条件将numpy数组转换为包含True或False的数组,然后将彼此相邻的True或False条目分组并计算每个组的长度.这是为了确定给定月份降水数据中干燥法术或湿法术的长度.
这是功能:
import itertools
def spell(X, kind='wet', how='mean', threshold=0.5):
if kind=='wet':
condition = X>threshold
else:
condition = X<=threshold
length = [sum(1 if x==True else nan for x in group) for key,group in itertools.groupby(condition) if key]
if not length:
res = 0
elif how=='mean':
res = np.mean(length)
else:
res = np.max(length)
return res
因此,基本上可以选择确定湿法或干法术的平均长度或最大长度,给出一系列大量的降水数据,默认参数设置为湿法术的平均长度.
我将此函数与pandas一起应用于历史记录的每个月:
#Create example dataframe
np.random.seed(1324)
idx = pd.DatetimeIndex(start='1960-01-01', periods=100, freq='d')
values = np.random.random(100)
df = pd.DataFrame(values, index=idx)
#Apply function
df.resample('M', how=spell)
而我得到的是:
0
1960-01-31 1.555556
1960-02-29 1.500000
1960-03-31 1.777778
1960-04-30 6.000000
这是完美的,但我希望能够在运行中稍微更改此函数的默认值,以便我可以使用df.resample()的其他选项.我已经研究过functools.partial()但是这只是一个解决方案,用于明确设置输入参数的情况. spell(kind =’dry’,how =’max’,threshold = 0.7).有没有办法改变函数的默认参数,以便它们不需要在文字之后显式设置,以便我可以将它与df.resample()一起使用?