简体   繁体   English

在熊猫的时间间隔上拆分系列?

[英]Split a series on time gaps in pandas?

Is it possible to split a time series on it's gaps.是否有可能在它的差距上分割时间序列。 For example, suppose we had the following:例如,假设我们有以下内容:

rng2011 = pd.date_range('1/1/2011', periods=72, freq='H')
rng2012 = pd.date_range('1/1/2012', periods=72, freq='H')
Y = rng2011.union(rng2012)

Is it possible to look for gaps of a year or more, and split the data frame on them?是否可以查找一年或更长时间的间隔,并在它们上拆分数据框?

I imagine this would go something like:我想这会是这样的:

Y.groupby(Y.map(lambda x: x.year))

Except that this splits on the year date, and I'm interested in specifying an interval gap rather than the year attribute of the row.除了这在年份日期拆分之外,我对指定间隔间隙而不是行的年份属性感兴趣。

The application is I've got trip logs from a gps, but no delineation of when one trip ended and another began.应用程序是我从 gps 获得了旅行日志,但没有描述一次旅行何时结束和另一次旅行开始的时间。 I'd like to split on gaps of ten minutes or longer.我想在十分钟或更长时间的间隙进行拆分。

Assuming Y is a column in your dataframe, one way is to use diff and cumsum :假设 Y 是数据框中的一列,一种方法是使用diffcumsum

df = DataFrame(Y)
df[1] = df[0].diff() > 600000000000.0 #nanoseconds in ten minutes
df[1] = df[1].cumsum()
df.groupby(1)

Note: If you use the number of nanoseconds in 72 hours it'll split into two groups.注意:如果您使用 72 小时内的纳秒数,它将分为两组。

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

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