[英]resample in pandas with the method in a variable
Pandas changed its resample API on version 18.1. Pandas在版本18.1上更改了其重采样API。 The reduction methods are no longer a argument to the resample method, but they are their own methods.
简化方法不再是重新采样方法的参数,而是它们自己的方法。
Example: 例:
import pandas as pd
import numpy as np
rng = pd.date_range('1/1/2012', periods=100, freq='S')
ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)
#Old API
ts.resample('5Min', how='sum')
#New API
ts.resample('5Min').sum()
I had some code that acted like this: 我有一些行为像这样的代码:
def my_func(my_series, how="sum"):
#Do some stuff before
my_series.resample('5Min' how=how)
How do I make this with the new API? 如何使用新API实现此目的? I want
my_func
to be able to call the resample method with different reduction methods. 我希望
my_func
能够使用不同的还原方法调用resample方法。
One answer already covers the case when the "how" is a just an aggregation function. 一个答案已经涵盖了“如何”只是一个聚合函数的情况。 I had more in mind cases where we want to perform upsampling.
我有更多想要进行上采样的案例。
Eg: 例如:
#Old API:
ts.resample('250L', fill_method='ffill')
#New API
ts.resample('250L').ffill()
Note that on my real code I have something more close to this: 请注意,在我的真实代码中,我有更接近这一点:
def my_func(dummy_df, freq="10Min", how="last", label="right", closed="right", fill_method="ffill"):
dummy_df.resample(freq, how=how, label=label, closed=closed, fill_method=fill_method)
and want to write it again with the new API. 并希望使用新API再次编写它。
Confusingly the documentation still (26.07.2016) has this line: 令人困惑的文档仍然(26.07.2016)有这一行:
Any function available via dispatching can be given to the how parameter by name, including sum, mean, std, sem, max, min, median, first, last, ohlc.
通过调度可用的任何函数可以通过名称给出how参数,包括sum,mean,std,sem,max,min,median,first,last,ohlc。
But the how
parameter is supposed to become deprecated. 但是
how
参数应该被弃用。
Solution with Resampler.agg
: 使用
Resampler.agg
解决方案:
print (ts.resample('5Min').agg('sum'))
print (ts.resample('5Min').sum())
2012-01-01 24223
Freq: 5T, dtype: int32
print (ts.resample('5Min').agg('sum'))
2012-01-01 24223
Freq: 5T, dtype: int32
So custom function is: 所以自定义功能是:
def my_func(my_series, how="sum"):
#Do some stuff before
return my_series.resample('5Min').agg(how)
print (my_func(ts))
2012-01-01 24223
Freq: 5T, dtype: int32
segregate how
and fill_method
and pass them through getattr
: 隔离
how
和fill_method
并通过getattr
传递它们:
def my_func(dummy_df, freq="10Min", how="last",
label="right", closed="right", fill_method="ffill"):
resample = dummy_df.resample(freq, label=label, closed=closed)
return getattr(getattr(resample, how)(), fill_method)()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.