编程语言
首页 > 编程语言> > python中的聚合时间序列

python中的聚合时间序列

作者:互联网

我们如何按小时或精细粒度汇总时间序列?如果我有如下的时间序列,那么我希望按小时聚合值.大熊猫是否支持它,或者在python中有一种漂亮的方式吗?

timestamp, value
2012-04-30T22:25:31+00:00, 1
2012-04-30T22:25:43+00:00, 1
2012-04-30T22:29:04+00:00, 2
2012-04-30T22:35:09+00:00, 4
2012-04-30T22:39:28+00:00, 1
2012-04-30T22:47:54+00:00, 8
2012-04-30T22:50:49+00:00, 9
2012-04-30T22:51:57+00:00, 1
2012-04-30T22:54:50+00:00, 1
2012-04-30T22:57:22+00:00, 0
2012-04-30T22:58:38+00:00, 7
2012-04-30T23:05:21+00:00, 1
2012-04-30T23:08:56+00:00, 1

我还尝试通过调用以确保我的数据框中有正确的数据类型:

  print data_frame.dtypes

我得到以下内容

ts     datetime64[ns]
val             int64

当我在数据框上调用group by时

grouped = data_frame.groupby(lambda x: x.minute)

我收到以下错误:

grouped = data_frame.groupby(lambda x: x.minute)
AttributeError: 'int' object has no attribute 'minute'

解决方法:

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.resample.html DataFrame.resample方法.您可以在此处指定聚合方式,在您的情况下为sum.

data_frame.resample("1Min", how="sum")

http://pandas.pydata.org/pandas-docs/dev/timeseries.html#up-and-downsampling

标签:python,pandas,time-series,timeserieschart
来源: https://codeday.me/bug/20190702/1360912.html