pandas 如何使用熊猫按 10 分钟对时间序列进行分组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32147224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to groupby time series by 10 minutes using pandas?
提问by Hello lad
Have a time series(ts) indexed by DatatimeIndex, want to group it by 10 minutes
有一个由 DatatimeIndex 索引的时间序列(ts),想要按 10 分钟对其进行分组
index x y z
ts1 ....
ts2 ....
...
I know how to group by 1 minute
我知道如何按 1 分钟分组
def group_by_minute(timestamp):
year = timestamp.year
month = timestamp.month
day = timestamp.day
hour = timestamp.hour
minute = timestamp.minute
return datetime.datetime(year, month, day, hour, minute)
then
然后
ts.groupby(group_by_minute, axis=0)
my customized function (roughly)
我的自定义功能(大致)
def my_function(group):
first_latitude = group['latitude'].sort_index().head(1).values[0]
last_longitude = group['longitude'].sort_index().tail(1).values[0]
return first_latitude - last_longitude
so the ts DataFrame should definitely contains 'latitude' and 'longitude' columns
所以 ts DataFrame 绝对应该包含“纬度”和“经度”列
When using TimeGrouper
使用 TimeGrouper 时
ts.groupby(pd.TimeGrouper(freq='100min')).apply(my_function)
I got the following errors,
我收到以下错误,
TypeError: cannot concatenate a non-NDFrame object
回答by CT Zhu
There is a pandas.TimeGrouperfor this sort of thing, what you described would be some thing like:
pandas.TimeGrouper这种事情有一个,你所描述的将是这样的:
agg_10m = df.groupby(pd.TimeGrouper(freq='10Min')).aggregate(numpy.sum) #or other function
回答by Andrew L
I know this is old but pd.Grouper() will also accomplish this:
我知道这是旧的,但 pd.Grouper() 也将实现这一点:
agg_10m = df.groupby(pd.Grouper(freq='10Min')).aggregate(numpy.sum)

