Hello lad
Hello lad

Reputation: 18790

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

Upvotes: 17

Views: 27441

Answers (2)

Andrew L
Andrew L

Reputation: 7038

I know this is old but pd.Grouper() will also accomplish this:

agg_10m = df.groupby(pd.Grouper(freq='10Min')).aggregate(numpy.sum)

Upvotes: 19

CT Zhu
CT Zhu

Reputation: 54340

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

Upvotes: 21

Related Questions