Python 试图按时筛选数据时,Get-TypeError(';索引必须是DatetimeIndex';)

Python 试图按时筛选数据时,Get-TypeError(';索引必须是DatetimeIndex';),python,pandas,Python,Pandas,以下是csv数据的结构: Date_Time Open High Low Close Volume 2015-05-21 15:30 2128.00 2132.00 2127.25 2128.50 160643 2015-05-21 14:30 2129.25 2130.25 2126.25 2128.25 68195 2015-05-21 13:30 2128.50 2129.50 2125.75

以下是csv数据的结构:

Date_Time         Open     High     Low      Close     Volume        
2015-05-21 15:30  2128.00  2132.00  2127.25  2128.50  160643
2015-05-21 14:30  2129.25  2130.25  2126.25  2128.25   68195
2015-05-21 13:30  2128.50  2129.50  2125.75  2129.00   59661
2015-05-21 12:30  2129.75  2130.75  2128.00  2128.25   40547
2015-05-21 11:30  2130.00  2130.50  2127.75  2129.50   73274
我想切片这个数据帧并及时过滤,显示09:30到10:30(一小时)之间的所有日子

我试过这个:

    df_initial_balance = ESData.between_time(start_time="09:30",end_time="10:30")
但是得到这个错误:

回溯(最近一次呼叫最后一次): 高开低闭卷 文件“C:/Users/tmgike/Dropbox/anders/Trading/Python/Pandas/range_analysis_ES.py”,第8行,在

Date_Time                                                   
    df_initial_balance = ESData.between_time(start_time="09:30",end_time="10:30")
2015-05-21 15:30  2128.00  2132.00  2127.25  2128.50  160643
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 2992, in between_time
2015-05-21 14:30  2129.25  2130.25  2126.25  2128.25   68195
2015-05-21 13:30  2128.50  2129.50  2125.75  2129.00   59661
2015-05-21 12:30  2129.75  2130.75  2128.00  2128.25   40547
2015-05-21 11:30  2130.00  2130.50  2127.75  2129.50   73274
    raise TypeError('Index must be DatetimeIndex')
TypeError: Index must be DatetimeIndex

我在Stackoverflow上查找了DatetimeIndex,但在Datetime列上尝试按时筛选时,找不到类似的问题。

您需要传递
parse_dates=[0]

In [170]:
t="""Date_Time,Open,High,Low,Close,Volume        
2015-05-21 15:30,2128.00,2132.00,2127.25,2128.50,160643
2015-05-21 14:30,2129.25,2130.25,2126.25,2128.25,68195
2015-05-21 13:30,2128.50,2129.50,2125.75,2129.00,59661
2015-05-21 12:30,2129.75,2130.75,2128.00,2128.25,40547
2015-05-21 11:30,2130.00,2130.50,2127.75,2129.50,73274"""
​
ESData=pd.read_csv(io.StringIO(t), index_col="Date_Time", parse_dates=[0])
df_initial_balance = ESData.between_time(start_time="12:30",end_time="14:30")
df_initial_balance

Out[170]:
                        Open     High      Low    Close  Volume        
Date_Time                                                              
2015-05-21 14:30:00  2129.25  2130.25  2126.25  2128.25           68195
2015-05-21 13:30:00  2128.50  2129.50  2125.75  2129.00           59661
2015-05-21 12:30:00  2129.75  2130.75  2128.00  2128.25           40547

您可以尝试一下
ESData=pd.read\u csv('ES-60min-Data.csv',index\u col=“Date\u Time”,sep=“;”,parse\u dates=[0])
这是一个csv文件,您没有指出我的答案或我的评论是否适合您,问题是,尽管您已经传递了参数以使用该列作为索引,它需要解析它才能生成数据类型DateTime您能解释一下该命令的作用吗?我指的是
parse_dates=[0]
@m-T-A这里我告诉
pandas
要解析csv中的第一列,
parse_dates
接受一个布尔值、名称列表或顺序位置列表以及一个dict,请参见
In [170]:
t="""Date_Time,Open,High,Low,Close,Volume        
2015-05-21 15:30,2128.00,2132.00,2127.25,2128.50,160643
2015-05-21 14:30,2129.25,2130.25,2126.25,2128.25,68195
2015-05-21 13:30,2128.50,2129.50,2125.75,2129.00,59661
2015-05-21 12:30,2129.75,2130.75,2128.00,2128.25,40547
2015-05-21 11:30,2130.00,2130.50,2127.75,2129.50,73274"""
​
ESData=pd.read_csv(io.StringIO(t), index_col="Date_Time", parse_dates=[0])
df_initial_balance = ESData.between_time(start_time="12:30",end_time="14:30")
df_initial_balance

Out[170]:
                        Open     High      Low    Close  Volume        
Date_Time                                                              
2015-05-21 14:30:00  2129.25  2130.25  2126.25  2128.25           68195
2015-05-21 13:30:00  2128.50  2129.50  2125.75  2129.00           59661
2015-05-21 12:30:00  2129.75  2130.75  2128.00  2128.25           40547