数据未按日期时间正确分组

Data not grouping correctly with datetime

我有以下代码:

import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import fxcmpy
import numpy as np
symbols = con.get_instruments()

ticker = 'NGAS'
start = datetime.datetime(2015,1,1)
end = datetime.datetime.today()
data = con.get_candles(ticker, period='m1', number=10000)
data.index = pd.to_datetime(data.index, format ='%Y-%m-%d %hh:%mm %s')
data.index = pd.to_datetime(data.index, format ='%Y-%m-%d %hh:%mm %s')
data['hour'] = data.index.hour
data['minute'] = data.index.minute

data 生成以下内容:

    bidopen bidclose    bidhigh bidlow  askopen askclose    askhigh asklow  tickqty hour    minute
date                                            
2019-12-05 07:00:00 2.4230  2.4280  2.4300  2.422   2.4305  2.4360  2.439   2.4295  47  7   0
2019-12-05 07:01:00 2.4280  2.4265  2.4270  2.426   2.4360  2.4340  2.436   2.4340  10  7   1
2019-12-05 07:02:00 2.4265  2.4295  2.4300  2.426   2.4340  2.4370  2.438   2.4340  35  7   2
2019-12-05 07:03:00 2.4295  2.4285  2.4300  2.428   2.4370  2.4360  2.438   2.4360  20  7   3
2019-12-05 07:04:00 2.4285  2.4350  2.4360  2.428   2.4360  2.4425  2.444   2.4360  50  7   4
... ... ... ... ... ... ... ... ... ... ... ...
2019-12-17 15:07:00 2.3335  2.3340  2.3345  2.332   2.3410  2.3415  2.342   2.3395  94  15  7
2019-12-17 15:08:00 2.3340  2.3345  2.3355  2.334   2.3415  2.3420  2.344   2.3415  22  15  8
2019-12-17 15:09:00 2.3345  2.3335  2.3345  2.332   2.3420  2.3410  2.342   2.3410  15  15  9
2019-12-17 15:10:00 2.3335  2.3325  2.3345  2.331   2.3410  2.3400  2.342   2.3390  72  15  10
2019-12-17 15:11:00 2.3325  2.3270  2.3325  2.326   2.3400  2.3345  2.340   2.3335  99  15  11

在上面的table小时从7开始到15结束。但是当我运行以下代码时,小时从0开始到59结束。这是为什么?

df = data.groupby(['hour', 'minute']).mean()

        bidopen bidclose    bidhigh bidlow  askopen askclose    askhigh asklow  tickqty
hour    minute                                  
0   0   2.302786    2.303500    2.304286    2.302071    2.310571    2.311214    2.312000    2.310143    16.285714
1   2.294917    2.294333    2.295250    2.293583    2.302667    2.302000    2.303333    2.301333    14.500000
2   2.283000    2.283333    2.283833    2.282333    2.290667    2.290833    2.292000    2.290167    18.666667
3   2.298417    2.298833    2.299167    2.297833    2.305917    2.306333    2.307000    2.305917    14.833333
4   2.283583    2.284000    2.284250    2.283000    2.291083    2.291750    2.292167    2.291083    14.166667
... ... ... ... ... ... ... ... ... ... ...
23  55  2.285500    2.285800    2.286600    2.284700    2.293100    2.293400    2.294300    2.292600    10.400000
56  2.303800    2.304000    2.304600    2.303300    2.311400    2.311700    2.312500    2.311000    11.200000
57  2.268700    2.268400    2.268900    2.268100    2.276200    2.276100    2.276700    2.275900    5.800000
58  2.302857    2.303000    2.303286    2.302357    2.310571    2.310571    2.311214    2.310286    8.000000
59  2.321300    2.321000    2.321700    2.320400    2.328900    2.328900    2.329500    2.328700    8.400000

我想要做的是按小时对数据进行分组,从 7 开始到 15 结束,然后我想要它的 mean() 。所以从第 7 小时到第 15 小时的 mean()。

-- 编辑 1: 如何将小时和日期设置为索引?

data.set_index('minute', inplace = True)
data.set_index('hour', inplace = True)

给我一个错误

首先,您看到的是一个多索引。您看到的小时数从 0 到 23,分钟数从 0 到 59。

如果您想要每小时的平均值,您只需要:

data.groupby(['hour']).mean().

如果您确实选择按其他数量分组,例如在 data.groupby(['hour','minute']).mean() 中,调用 .reset_index() 可能有助于避免多索引的混淆。

(例如 df = data.groupby(['hour','minute']).mean().reset_index()

您看到的结果是正确的:

第一行的日期是 12 月 5 日,最后一行的日期是 12 月 17 日,所以中间有很多行,时间是下午 3 点之后或早上 7 点之前。

尝试df[df['hour']>15].head()查看当天下午 3 点以后的一些台词

更新:

要获得 7 - 15 小时的平均值,请先查看下面的示例代码

df = pd.DataFrame()

df['hour']=np.array([15,12,10,6,4,19,15,12,10])
df['price']=np.array([1,2,3,4,5,6,7,8,9])

df[(df['hour']>=7)&(df['hour']<=15)].mean().price

哪个returns

5.0

或按小时计算的平均值

df[(df['hour']>=7)&(df['hour']<=15)].groupby('hour').mean()

哪个returns

    price
hour    
10  6
12  5
15  4

也许 data.index = pd.to_datetime(data.index, format ='%Y-%m-%d %hh:%mm %s') 应该改为 data.index = pd.to_datetime(data.index, format ='%Y-%m-%d %H:%M %S') 时分秒!

%hh:%mm %s 在 python 日期时间中不受支持,而不是:

data.index = pd.to_datetime(data.index, format ='%Y-%m-%d %hh:%mm %s')

使用:

data.index = pd.to_datetime(data.index, format ='%Y-%m-%d %H:%M %S')