How to change pandas resampling on a UTC OHLC Time Series based on Daylight Saving

867 views Asked by At

I have a 1-minute OHLC CSV file with date using UTC

df = pd.read_csv('...', usecols=['Date','Open','High','Low','Close'],
    index_col=['Date'], parse_dates=['Date'])

I am resampling it using:

ohlc_head = {'Open':'first', 'High':'max', 'Low':'min', 'Close': 'last'}
df_resamples = df.resample('4h', base=21).agg(ohlc_head).dropna(how='any')

This will resample from Sunday 21:00 onward, so it is 21:00, 01:00, 05:00, ..., until Friday 17:00 Where 21:00 UTC is the market open time.

However, when there is daylight saving switch, the first minute in the week starts at Sunday 22:00 and finishes at 18:00.

How can you alternate the resampling to be at 22:00 when the first minute on Sunday starts at 22:00 and 21:00 when the first minute on Sunday start at 21:00? In brief, the resampling should start automatically at the first minute in the CSV (which is the start of the week on Sunday) and continue in the same pattern until it encounters another start and so on.

1

There are 1 answers

0
Adam On BEST ANSWER

I figured out an easy solution, which is localising time, resampling, then changing to UTC again:

df.index = df.index.tz_localize('UTC').tz_convert('Europe/London')
ohlc_dict = {'Open':'first', 'High':'max', 'Low':'min', 'Close': 'last'}
df = df.resample('4h', base=21).agg(ohlc_dict).dropna(how='any')
df.index = df.index.tz_convert('UTC')