Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify OHLC resample code as per deprecated warning

Tags:

python

pandas

Issue:

When working with market data and resampling intra-day data to the daily timeframe as follows:

ohlc_dict = {
'Open':'first',
'High':'max',
'Low':'min',
'Last': 'last',
'Volume': 'sum'}

data.resample('1D',how=ohlc_dict).tail().dropna()

                Open    High    Last    Low     Volume
    Timestamp                   
    2016-12-27  163.55  164.18  164.11  163.55  144793.00
    2016-12-28  164.18  164.33  164.22  163.89  215288.00
    2016-12-29  164.44  164.65  164.49  164.27  245538.00
    2016-12-30  164.55  164.56  164.18  164.09  286847.00

Which seems to gives me the output I need (still need to verify)...

I get the following warning:

FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...)..apply(<func>)

Question:

How would this resample code be replicated using the new syntax to align with the current best practice using apply?

What I have tried:

Just using data['Low'] as an example:

def ohlc (df):
    return df['Low'].min()

data.resample('1D').dropna().apply(ohlc,axis=1).tail(2)

Timestamp
2016-12-29   164.45
2016-12-30   164.26
dtype: float64

Does not give me the same results and Im not sure where to insert the apply.

Here is a slice of the data to test this with if required:

thanks

like image 238
nipy Avatar asked Dec 31 '16 11:12

nipy


1 Answers

.resample() works like groupby so you can pass that dictionary to resample().agg():

df.resample('1D').agg(ohlc_dict).tail().dropna()
Out: 
              Volume    Last    High    Open     Low
Timestamp                                           
2016-12-27  144793.0  164.11  164.18  163.55  163.55
2016-12-28  215288.0  164.22  164.33  164.18  163.89
2016-12-29  245538.0  164.49  164.65  164.44  164.27
2016-12-30  286847.0  164.18  164.56  164.55  164.09
like image 167
ayhan Avatar answered Nov 09 '22 07:11

ayhan