How to create a custom class pandas.tseries.offsets?

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

# set as 'constant' object shared by all codes in this script
BDAY_US = CustomBusinessDay(calender=USFederalHolidayCalendar())
sample_freq = '5min'
dates = pd.date_range(start='2015-01-01', end='2015-01-31', freq=BDAY_US).date
# exculde the 09:30:00 as it is included in the first time bucket
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()

enter image description here

, , 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
# hourly increment works nicely
print(time + bhour * 1)
2015-06-19 09:30:00
# but not at minute or second frequency
print(time + Minute(61))
2015-06-18 16:01:00
print(time + Second(60*60 + 1))
2015-06-18 16:00:01

, .

+4
1

, .

  • - .
  • , datetime ( ),

, 1, , , . 2 - :

1.

matplotlib , , ticker API.

import pandas as pd
import numpy as np
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# set as 'constant' object shared by all codes in this script
BDAY_US = CustomBusinessDay(calender=USFederalHolidayCalendar())
sample_freq = '5min'
dates = pd.date_range(start='2015-01-01', end='2015-01-31', freq=BDAY_US).date
# exculde the 09:30:00 as it is included in the first time bucket
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)

data_length = len(s)
s.index.name = 'date_time_index'
s.name='stock_price'
s_new = s.reset_index()

ax = s_new.plot(y='stock_price') #plot the data against the new linearised index...

def format_date(x,pos=None):
    thisind = np.clip(int(x+0.5), 0, data_length-1)
    return s_new.date_time_index[thisind].strftime('%Y-%m-%d %H:%M:%S')

ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))

fig = plt.gcf()
fig.autofmt_xdate()

plt.show()

: , , 16:00 09:00

zoomed out time series

zoomed in time series over a weekend

+3

All Articles