Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用变量中的方法在中重新采样_Python_Datetime_Pandas_Dataframe_Resampling - Fatal编程技术网

Python 使用变量中的方法在中重新采样

Python 使用变量中的方法在中重新采样,python,datetime,pandas,dataframe,resampling,Python,Datetime,Pandas,Dataframe,Resampling,Pandas在版本18.1上更改了其重采样API。还原方法不再是重采样方法的参数,而是它们自己的方法 例如: 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='su

Pandas在版本18.1上更改了其重采样API。还原方法不再是重采样方法的参数,而是它们自己的方法

例如:

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()
我有一些代码是这样的:

def my_func(my_series, how="sum"):
    #Do some stuff before
    my_series.resample('5Min' how=how)
如何使用新的API实现这一点?我希望
my_func
能够使用不同的简化方法调用重采样方法

其中一个已经涵盖了“如何”只是一个聚合函数的情况。我想到了更多我们想要执行上采样的情况

例如:

请注意,在我的真实代码中,我有一些更接近于此的内容:

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)
并希望用新的API再次编写它

令人困惑的是,still(2016年7月26日)有这样一行:

任何通过调度可用的函数都可以按名称指定how参数,包括sum、mean、std、sem、max、min、MEDIANT、first、last、ohlc

但是
how
参数应该被弃用。

解决方案包括:


因此,自定义函数是:

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

如何
填充方法
分开,并通过
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)()

非常简单和优雅!
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
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
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)()