【发布时间】: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 日期时间