asfreq和resample之间的区别 [英] Difference between asfreq and resample

查看:328
本文介绍了asfreq和resample之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您解释一下熊猫的asfreq和resample方法之间的区别?什么时候应该使用什么?

Can some please explain the difference between the asfreq and resample methods in pandas? When should one use what?

推荐答案

让我用一个例子来说明:

Let me use an example to illustrate:

# generate a series of 365 days
# index = 20190101, 20190102, ... 20191231
# values = [0,1,...364]
ts = pd.Series(range(365), index = pd.date_range(start='20190101', 
                                                end='20191231',
                                                freq = 'D'))
ts.head()

output:
2019-01-01    0
2019-01-02    1
2019-01-03    2
2019-01-04    3
2019-01-05    4
Freq: D, dtype: int64

现在,按季度对数据进行重新采样:

Now, resample the data by quarter:

ts.asfreq(freq='Q')

output:
2019-03-31     89
2019-06-30    180
2019-09-30    272
2019-12-31    364
Freq: Q-DEC, dtype: int64

asfreq()返回一个Series对象,其中包含每个季度的最后一天.

The asfreq() returns a Series object with the last day of each quarter in it.

ts.resample('Q')

output:
DatetimeIndexResampler [freq=<QuarterEnd: startingMonth=12>, axis=0, closed=right, label=right, convention=start, base=0]

重新采样返回DatetimeIndexResampler,您看不到实际的内容.将其视为groupby方法.它创建一个bins(组)列表:

Resample returns a DatetimeIndexResampler and you cannot see what's actually inside. Think of it as the groupby method. It creates a list of bins (groups):

bins = ts.resample('Q')
bin.groups

output:
 {Timestamp('2019-03-31 00:00:00', freq='Q-DEC'): 90,
 Timestamp('2019-06-30 00:00:00', freq='Q-DEC'): 181,
 Timestamp('2019-09-30 00:00:00', freq='Q-DEC'): 273,
 Timestamp('2019-12-31 00:00:00', freq='Q-DEC'): 365}

到目前为止,除了返回类型外,其他都没有什么不同.让我们计算每个季度的平均值:

Nothing seems different so far except for the return type. Let's calculate the average of each quarter:

# (89+180+272+364)/4 = 226.25
ts.asfreq(freq='Q').mean()

output:
226.25

应用mean()时,将输出所有值的平均值.请注意,这不是每个季度的平均值,而是每个季度最后一天的平均值.

When mean() is applied, it outputs the average of all the values. Note that this is not the average of each quarter, but the average of the last day of each quarter.

要计算每个季度的平均值:

To calculate the average of each quarter:

ts.resample('Q').mean()

output:
2019-03-31     44.5
2019-06-30    135.0
2019-09-30    226.5
2019-12-31    318.5

您可以使用resample()执行更强大的操作asfreq().

resample视为groupby +您可以在groupby之后调用的每个方法(例如,均值,求和,应用,命名).

Think of resample as groupby + every method that you can call after groupby (e.g. mean, sum, apply, you name it) .

asfreq视为具有有限fillna()功能的筛选器机制(在fillna()中,您可以指定limit,但asfreq()不支持它).

Think of asfreq as a filter mechanism with limited fillna() capabilities (in fillna(), you can specify limit, but asfreq() does not support it).

这篇关于asfreq和resample之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆