Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

python - 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. I'd like to split on gaps of ten minutes or longer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming Y is a column in your dataframe, one way is to use diff and cumsum:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...