【问题标题】:Get TypeError('Index must be DatetimeIndex') when trying to filter data on time尝试按时过滤数据时出现 TypeError('Index must be DatetimeIndex')
【发布时间】:2015-05-22 13:23:27
【问题描述】:

这是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(一小时)之间的所有日期。

import pandas as pd
import datetime

ESData=pd.read_csv('ES-60min-Data.csv', index_col="Date_Time",sep=";")
print ESData.head()

df_initial_balance = ESData.between_time(start_time="09:30",end_time="10:30")
print df_initial_balance.head()

我试过了:

    df_initial_balance = ESData.between_time(start_time="09:30",end_time="10:30")

但得到这个错误:

Traceback(最近一次调用最后一次): 开高低收盘量 文件“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 列时找不到类似的问题。

【问题讨论】:

  • 你可以试试ESData=pd.read_csv('ES-60min-Data.csv', index_col="Date_Time",sep=";", parse_dates=[0])
  • 你没有说明我的回答或我的评论是否适合你,关键是虽然你已经传递了参数以使用该列作为索引,但它需要解析它以使dtype 日期时间

标签: python pandas


【解决方案1】:

你需要通过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

【讨论】:

  • 您能解释一下该命令的作用吗?我指的是parse_dates=[0]
  • @M-T-A 在这里我告诉pandas 解析csv 中的第一列,parse_dates 接受一个布尔值、名称列表或序数位置列表和一个字典,请参见@ 987654321@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 2023-01-31
  • 2020-05-10
相关资源
最近更新 更多