问题描述
我有一个EURUSD货币的分钟价格数据集。我更喜欢使用的时间范围是“ 60min”,因此我将数据集重采样为1小时(如下所示)。但是,我也想用我的新数据集访问分钟数据。我正在寻找一种可以传递时间的方法,例如“ 2019-01-01 17:00:00”,并可以访问该小时内的所有分钟数据。我尝试切片原始的“ Rates” df,但收到错误消息“无法使用非整数键按位置索引编制索引”
什么是最好的方法?
#import libraries
import pandas as pd
from datetime import datetime
numpy as np
#read excel
Rates = pd.read_excel("test.xlsx")
#Set columns
Rates.columns = ["Date","Open","High","Low","Close","Volume"]
print(Rates)
Date Open High Low Close Volume
2019-01-01 17:03:00 1.14598 1.14607 1.14598 1.14607 0
2019-01-01 17:04:00 1.14607 1.14607 1.14606 1.14606 0
2019-01-01 17:05:00 1.14606 1.14621 1.14606 1.14621 0
2019-01-01 17:06:00 1.14619 1.14666 1.14604 1.14665 0
2019-01-01 17:07:00 1.14665 1.14672 1.14607 1.14607 0
2019-01-01 17:08:00 1.14607 1.14607 1.14606 1.14607 0
2019-01-01 17:09:00 1.14607 1.14617 1.14606 1.14606 0
2019-01-01 17:10:00 1.14607 1.14607 1.14607 1.14607 0
2019-01-01 17:11:00 1.14607 1.14617 1.14606 1.14607 0
2019-01-01 17:12:00 1.14607 1.14608 1.14607 1.14608 0
2019-01-01 17:13:00 1.14608 1.14608 1.14607 1.14607 0
2019-01-01 17:14:00 1.14607 1.14608 1.14607 1.14608 0
2019-01-01 17:15:00 1.14607 1.14608 1.14607 1.14607 0
2019-01-01 17:16:00 1.14607 1.14608 1.14607 1.14607 0
2019-01-01 17:17:00 1.14607 1.14607 1.14607 1.14607 0
2019-01-01 17:18:00 1.14606 1.14617 1.14606 1.14607 0
2019-01-01 17:19:00 1.14617 1.14617 1.14606 1.14606 0
2019-01-01 17:20:00 1.14607 1.14616 1.14606 1.14616 0
#Convert "Date" column to DateTime
DateTime = pd.to_datetime(Rates.Date)
#Set index to Datetime object
Rates.index = DateTime
#Remove prevIoUs "Date" Column
df = Rates.drop(["Date"],axis=1)
#Resample to hourly data
df = df.resample('1H').first()
print(df)
2019-01-01 17:00:00 1.14598 1.14607 1.14598 1.14607 0.0
2019-01-01 18:00:00 1.14605 1.14653 1.14566 1.14603 0.0
2019-01-01 19:00:00 1.14612 1.14612 1.14606 1.14606 0.0
2019-01-01 20:00:00 1.14561 1.14563 1.14559 1.14563 0.0
2019-01-01 21:00:00 1.14521 1.14524 1.14506 1.14512 0.0
#Now when I have the data resampled,I try to access all minute wise data by slicing.
Rates.iloc[str(df.index[-1])],buy i get the error message: "Cannot index by location index with a non-integer key"
我想要的输出是传递一个日期,例如:“ 2019-01-01 17:00:00”,然后访问该小时内的所有分钟数据。
非常感谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)