非连续盘中指数 [英] Non-consecutive intraday index

查看:101
本文介绍了非连续盘中指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与以下内容有关:

This question is related to : Python pandas, how to only plot a DataFrame that actually have the datapoint and leave the gap out

我想知道以日内分辨率产生不连续的DateTimeIndex的最简单方法,即仅在某些[证券交易所]时间之间保持样本,例如08:00-16:30,并且只提供了工作日,例如周一至周五奖励是允许提供有效日期的日历.

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.

在白天,周一至周五使用pandas.bdate_range()很容易.我想要的是盘中类似的东西,例如第二个分辨率,但不包括周六/周日.

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:

与以下内容相比(请注意, x标签会保留在第二个分辨率上,尽管此处仅显示日期-放大时可见):

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):

这不是实现此目标的唯一方法;请参阅链接的问题以获取替代建议(最简单的方法可能是将use_index=False参数用于pandas.Series.plot()).但是这个问题是关于创建一个非连续的DateTimeIndex的问题;我不是在寻求替代解决方案

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:

import pandas as pd
index = pd.date_range('2016-01-01', '2016-01-16', freq='1min')
index[(index.dayofweek <= 4) & (index.hour >= 8) & (index.hour <= 16)]

输出:

DatetimeIndex(['2016-01-01 08:00:00', '2016-01-01 08:01:00',
               '2016-01-01 08:02:00', '2016-01-01 08:03:00',
               '2016-01-01 08:04:00', '2016-01-01 08:05:00',
               '2016-01-01 08:06:00', '2016-01-01 08:07:00',
               '2016-01-01 08:08:00', '2016-01-01 08:09:00',
               ...
               '2016-01-15 16:50:00', '2016-01-15 16:51:00',
               '2016-01-15 16:52:00', '2016-01-15 16:53:00',
               '2016-01-15 16:54:00', '2016-01-15 16:55:00',
               '2016-01-15 16:56:00', '2016-01-15 16:57:00',
               '2016-01-15 16:58:00', '2016-01-15 16:59:00'],
              dtype='datetime64[ns]', length=5940, freq=None)

您可以通过向遮罩添加条件来包括日历:

You could include a calendar by adding a condition to the mask:

import numpy as np
np.in1d(index.date, calendar)

其中calendarnumpy对象的numpy数组.

这篇关于非连续盘中指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆