这是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")
但得到此错误:
追踪(最近一次通话): 打开高低收盘量 文件" 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,但在日期时间列上尝试按时过滤时找不到类似的问题。
答案 0 :(得分: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