简体   繁体   English

重新采样熊猫时间序列以获得 bin 名称的结束时间戳?

[英]Resample panda time series to have the end time stamp for the bin name?

I generate a sample 5 minute time series:我生成了一个样本 5 分钟时间序列:

index = pd.date_range('1/1/2000', periods=10, freq='5T')
data=range(10)
ser = pd.Series(data, index=index)

What it looks like:它的样子:

2000-01-01 00:00:00    0.0
2000-01-01 00:05:00    1.0
2000-01-01 00:10:00    2.0
2000-01-01 00:15:00    3.0
2000-01-01 00:20:00    4.0
2000-01-01 00:25:00    5.0
2000-01-01 00:30:00    6.0
2000-01-01 00:35:00    7.0
2000-01-01 00:40:00    8.0
2000-01-01 00:45:00    9.0
Freq: 5T, dtype: float64

What I need我需要的

I would like to turn this time series into a 15 minute one and have each 15 minute value be the mean of the 5 minute values observed in that 15 min period, ie我想把这个时间序列变成一个 15 分钟的时间序列,每个 15 分钟的值是在 15 分钟期间观察到的 5 分钟值的平均值,即

2000-01-01 00:15:00    2.0   # i.e. mean(1, 2, 3)
2000-01-01 00:30:00    5.0   # i.e. mean(4, 5, 6)
2000-01-01 00:45:00    8.0   # i.e. mean(7, 8, 9)

Things I tried我尝试过的事情

If I resample this data into 15 minute buckets and call mean i get:如果我将这些数据重新采样到 15 分钟的桶中并调用 mean 我得到:

ser.resample('15T').mean()

2000-01-01 00:00:00    1.0
2000-01-01 00:15:00    4.0
2000-01-01 00:30:00    7.0
2000-01-01 00:45:00    9.0

which is not computing the means I want.这不是计算我想要的手段。 If I add closed='right' to the resample call I get closer to the values I want but the timestamps are not right.如果我将closed='right'添加到 resample 调用中,我会更接近我想要的值,但时间戳不正确。

ser.resample('15T', closed='right').mean()
1999-12-31 23:45:00    0.0
2000-01-01 00:00:00    2.0
2000-01-01 00:15:00    5.0
2000-01-01 00:30:00    8.0
Freq: 15T, dtype: float64

Any suggestions?有什么建议?

You can use the label argument in resample ,您可以在resample 中使用label参数,

ser.resample('15T', label='right', closed='right').mean()

this shifts the label from the left (default) to the right of the resampled window.这会将标签从重新采样窗口的左侧(默认)移动到右侧。 This is a more succinct than my somewhat clunky comment.这比我有点笨拙的评论更简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM