9

I'd like to create multiple columns while resampling a pandas DataFrame like the built-in ohlc method.

def mhl(data):
    return pandas.Series([np.mean(data),np.max(data),np.min(data)],index = ['mean','high','low'])

ts.resample('30Min',how=mhl)

Dies with

Exception: Must produce aggregated value

Any suggestions? Thanks!

1 Answer 1

8

You can pass a dictionary of functions to the resample method:

In [35]: ts
Out[35]:
2013-01-01 00:00:00     0
2013-01-01 00:15:00     1
2013-01-01 00:30:00     2
2013-01-01 00:45:00     3
2013-01-01 01:00:00     4
2013-01-01 01:15:00     5
...
2013-01-01 23:00:00    92
2013-01-01 23:15:00    93
2013-01-01 23:30:00    94
2013-01-01 23:45:00    95
2013-01-02 00:00:00    96
Freq: 15T, Length: 97

Create a dictionary of functions:

mhl = {'m':np.mean, 'h':np.max, 'l':np.min}

Pass the dictionary to the how parameter of resample:

In [36]: ts.resample("30Min", how=mhl)
Out[36]:
                      h     m   l
2013-01-01 00:00:00   1   0.5   0
2013-01-01 00:30:00   3   2.5   2
2013-01-01 01:00:00   5   4.5   4
2013-01-01 01:30:00   7   6.5   6
2013-01-01 02:00:00   9   8.5   8
2013-01-01 02:30:00  11  10.5  10
2013-01-01 03:00:00  13  12.5  12
2013-01-01 03:30:00  15  14.5  14
3
  • 2
    It's about 10x faster to use "mean" than to use np.mean. Same goes for 'min' and 'max'
    – Tom Leys
    Sep 20, 2013 at 3:43
  • 2
    Is there a way to specify a default for most columns (e.g., sum instead of mean) and then override the method for a single column? Jan 15, 2014 at 19:02
  • Neat trick: you can even pass a dictionary (for the columns) of dictionary of functions, like so: mhl = {'data_column_1': {'resultA': np.mean, 'resultB': max}, 'data_column_2': {'resultC': min, 'resultD': sum}}
    – Def_Os
    Dec 19, 2015 at 6:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.