This question is related to : Python pandas, how to only plot a DataFrame that actually have the datapoint and leave the gap out
I'd like to know the easiest way to produce non-consecutive DateTimeIndex at intra-day resolution, that only maintains samples between certain [stock exchange] times e.g. 08:00-16:30, and has only given weekdays e.g. Mon-Fri. A bonus would be to be allow provision of a calendar of valid dates.
At the day range, it's easy to do with pandas.bdate_range()
for Mon-Fri. What I'd like is something analogous at intraday e.g. second resolution, but doesn't include Saturday/Sunday.
The point of this is to be able to graph consecutive days of financial time series without 'gaps', while maintaining the labels. i.e. this:
vs the below (note that x labels are persisted, at the second resolution, although only dates are shown here - when you zoom in the time becomes visible):
This is not the only way to achieve this; see the linked questions for alternative suggestions (the easiest probably being to use the use_index=False
parameter to pandas.Series.plot()
). But this question is in reference to the creation of a non-consecutive DateTimeIndex
; I'm not asking for alternatives solutions
You could create a full intraday index and filter out nights and week-ends:
Output:
You could include a calendar by adding a condition to the mask:
where
calendar
would be anumpy
array ofdatetime
objects.