I want to find a way to create a custom class pandas.tseries.offsetswith a frequency of 1 second for trading hours. The main requirement here is that the time offset object would be smart enough to know that the next second of “2015-06-18 16:00:00” would be “2015-06-19 09:30:00 or 09: 30: 01 ', and the time delta calculated from these two time stamps will be exactly 1 s (user offset 1s, similar BDay(1)to the frequency of the working day) instead of the closing time.
The reason is that when plotting the pd.Series chart for intraday data for several trading days, see the simulation example below, there are many “step lines” (linear interpolation) between closed and open prices the next day to represent the length of the closing time. Is there any way to get rid of this? I look at the source code pandas.tseries.offsets, and search pd.tseries.offsets.BusinessHourand pd.tseries.offsets.BusinessMixincan help, but I do not know how to use them.
import pandas as pd
import numpy as np
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
BDAY_US = CustomBusinessDay(calender=USFederalHolidayCalendar())
sample_freq = '5min'
dates = pd.date_range(start='2015-01-01', end='2015-01-31', freq=BDAY_US).date
times = pd.date_range(start='09:30:00', end='16:00:00', freq=sample_freq).time[1:]
time_stamps = [dt.datetime.combine(date, time) for date in dates for time in times]
s = pd.Series(np.random.randn(len(time_stamps)).cumsum() + 100, index=time_stamps)
s.plot()

, , reset_index(), , ( ). x . - , matplotlib?
. - BusinessHour() , . (): BusinessHour , 1 ? , CustomBusinessDay?
BusinessHour()
from pandas.tseries.offsets import *
bhour = BusinessHour(start='09:30', end='16:00')
time = pd.Timestamp('2015-06-18 15:00:00')
print(time)
2015-06-18 15:00:00
print(time + bhour * 1)
2015-06-19 09:30:00
print(time + Minute(61))
2015-06-18 16:01:00
print(time + Second(60*60 + 1))
2015-06-18 16:00:01
, .