简体   繁体   中英

Resample pandas time series 30 mins for 9:15 as start time

I am trying to resample OHLC data to 30 mins. The market data starts at at 9:15 and I would like the resampled time to have 9:15-9:45 and so on. But I am able to get the data resampled as 9:00-9:30

Paste Bin link to 1 min market data

pd.DataFrame(download_data).set_index('date'['close'].resample('30T').ohlc()

As you see in the picture the start time is 9:00 and not 9:15...

还有另一种方法,您可以使用resamplebase参数:

pd.DataFrame(download_data).set_index('date'['close'].resample('30T', base=15).ohlc()

Solution is add parameter loffset in resample :

loffset : timedelta

Adjust the resampled time labels

df = (pd.DataFrame(download_data)
        .set_index('date')['close']
        .resample('30T', loffset='15min')
        .ohlc())
print (df)

                               open      high      low     close
date                                                            
2018-11-05 09:15:00+05:30  25638.25  25641.85  25589.3  25630.00
2018-11-05 09:45:00+05:30  25622.00  25745.00  25622.0  25714.85
2018-11-05 10:15:00+05:30  25720.05  25740.00  25692.9  25717.00
2018-11-05 10:45:00+05:30  25698.30  25744.75  25667.9  25673.95
2018-11-05 11:15:00+05:30  25680.30  25690.45  25642.9  25655.90

Though the answers provided here work as expected, it must be noted that both and are deprecated since version 1.1.0 . The best and simplest way now is

pd.DataFrame(download_data).set_index('date'['close']).resample('30T', origin='start').ohlc() 

This will set the start time to the first timestamp available in the dataframe.

@TFA the use of origin='start' may not be that useful esp. if our start time does not fit in that rule of starting from 9:15 am, eg if we are trying to convert 1 min data to 15 mins, and we give data from 9:37 am, the 15mins candles will start from 9:37 am, which is not what we need.

The better solution that I could find was to use origin = <timestamp> , so something like origin = datetime.fromisoformat('1970-01-01 09:15:00+05:30') does the magic.

Overall code:

pd.DataFrame(download_data).set_index('date'['close']).resample('30T', origin=datetime.fromisoformat('1970-01-01 09:15:00+05:30')).ohlc() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM