pandas 数据框中使用日期时间的部分 for 循环
Partail for-loop in pandas dataframe using datetime
我在数据框中有 Apple 的 1 分钟代码信息:
Local time Open High Low Close Volume
0 2018-04-19 15:00:00 46.707 46.708 46.687 46.687 0.0150
1 2018-04-19 15:01:00 46.688 46.688 46.667 46.688 0.0320
2 2018-04-19 15:02:00 46.687 46.728 46.677 46.728 0.0091
3 2018-04-19 15:03:00 46.727 46.728 46.708 46.717 0.0332
4 2018-04-19 15:04:00 46.708 46.718 46.677 46.677 0.0243
我已使用 pd.to_datetime(df['Local time'])
将“当地时间”列转换为日期时间。我想单独经历每一天来回测策略。但我不知道如何在日期更改定义的时间循环遍历 df 的一个块。我尝试使用一些 for 循环,但它们没有用,因为某些天的交易分钟数明显不同(不是 390):
index = 390 #Number of traded minutes on most days
rows = 286155 #number of rows in the dataset
for x in range(286155/390):
index = index * x
index2 = index * (x-1)
for y in df[index2:index]:
'''Strategy to be Executed for that day'''
我怎样才能实现我想做的事情?
如@Ben.T所建议:
for dt, df in data.groupby(data["Local time"].dt.date):
print(f"\n[{dt}]")
print(df.head())
# do stuff here
[2021-04-16]
Local time Value
0 2021-04-16 00:00:00 28.15
1 2021-04-16 00:01:00 25.33
2 2021-04-16 00:02:00 82.04
3 2021-04-16 00:03:00 17.81
4 2021-04-16 00:04:00 80.71
[2021-04-17]
Local time Value
1440 2021-04-17 00:00:00 67.72
1441 2021-04-17 00:01:00 52.91
1442 2021-04-17 00:02:00 26.40
1443 2021-04-17 00:03:00 69.11
1444 2021-04-17 00:04:00 91.88
[2021-04-18]
Local time Value
2880 2021-04-18 00:00:00 13.03
2881 2021-04-18 00:01:00 53.42
2882 2021-04-18 00:02:00 9.28
2883 2021-04-18 00:03:00 77.74
2884 2021-04-18 00:04:00 24.91
我在数据框中有 Apple 的 1 分钟代码信息:
Local time Open High Low Close Volume
0 2018-04-19 15:00:00 46.707 46.708 46.687 46.687 0.0150
1 2018-04-19 15:01:00 46.688 46.688 46.667 46.688 0.0320
2 2018-04-19 15:02:00 46.687 46.728 46.677 46.728 0.0091
3 2018-04-19 15:03:00 46.727 46.728 46.708 46.717 0.0332
4 2018-04-19 15:04:00 46.708 46.718 46.677 46.677 0.0243
我已使用 pd.to_datetime(df['Local time'])
将“当地时间”列转换为日期时间。我想单独经历每一天来回测策略。但我不知道如何在日期更改定义的时间循环遍历 df 的一个块。我尝试使用一些 for 循环,但它们没有用,因为某些天的交易分钟数明显不同(不是 390):
index = 390 #Number of traded minutes on most days
rows = 286155 #number of rows in the dataset
for x in range(286155/390):
index = index * x
index2 = index * (x-1)
for y in df[index2:index]:
'''Strategy to be Executed for that day'''
我怎样才能实现我想做的事情?
如@Ben.T所建议:
for dt, df in data.groupby(data["Local time"].dt.date):
print(f"\n[{dt}]")
print(df.head())
# do stuff here
[2021-04-16]
Local time Value
0 2021-04-16 00:00:00 28.15
1 2021-04-16 00:01:00 25.33
2 2021-04-16 00:02:00 82.04
3 2021-04-16 00:03:00 17.81
4 2021-04-16 00:04:00 80.71
[2021-04-17]
Local time Value
1440 2021-04-17 00:00:00 67.72
1441 2021-04-17 00:01:00 52.91
1442 2021-04-17 00:02:00 26.40
1443 2021-04-17 00:03:00 69.11
1444 2021-04-17 00:04:00 91.88
[2021-04-18]
Local time Value
2880 2021-04-18 00:00:00 13.03
2881 2021-04-18 00:01:00 53.42
2882 2021-04-18 00:02:00 9.28
2883 2021-04-18 00:03:00 77.74
2884 2021-04-18 00:04:00 24.91