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
408 views
in Technique[技术] by (71.8m points)

python - How to groupby time series by 10 minutes using pandas

Have a time series(ts) indexed by DatatimeIndex, want to group it by 10 minutes

index   x  y  z

ts1     ....
ts2     ....
...

I know how to group by 1 minute

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

When using TimeGrouper

   ts.groupby(pd.TimeGrouper(freq='100min')).apply(my_function)

I got the following errors,

TypeError: cannot concatenate a non-NDFrame object
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

There is a pandas.TimeGrouper for this sort of thing, what you described would be some thing like:

agg_10m = df.groupby(pd.TimeGrouper(freq='10Min')).aggregate(numpy.sum) #or other function

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

...