Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果时间相同,是否将行合并为一个ohlc行?_Python_Pandas_Sqlite - Fatal编程技术网

Python 如果时间相同,是否将行合并为一个ohlc行?

Python 如果时间相同,是否将行合并为一个ohlc行?,python,pandas,sqlite,Python,Pandas,Sqlite,我一直在尝试将数据从tick数据库重新采样到另一个名为min的数据库 我将加载到pandas中,将两个数据库的转换成excel 在min数据帧中: timestamp open high low close 47759 2019-11-04 14:23:00 30468.00 30473.90 30440.15 30445.00 47758 2019-11-04 14:23:00 30468.00 30473.90

我一直在尝试将
数据从tick
数据库
重新采样到另一个名为
min
的数据库

我将
加载到
pandas
中,将两个数据库的
转换成excel

min
数据帧中:

       timestamp           open      high      low       close
47759 2019-11-04 14:23:00  30468.00  30473.90  30440.15  30445.00
47758 2019-11-04 14:23:00  30468.00  30473.90  30440.15  30450.00
47758 2019-11-04 14:23:00  30468.00  30473.90  30440.15  30455.00
47758 2019-11-04 14:23:00  30468.00  30473.90  30440.15  30460.00
47758 2019-11-04 14:23:00  30468.00  30479.00  30440.15  30479.00

当然,上面有很多行,它们有不同的时间戳

我想知道如何将所有这些组合在一起,只得到一行正确的
打开、高、低、关闭


这一过程不会很耗时,因为信号处理必须这样做。

您可以使用
.agg
并在初始
groupby
之后传递一个包含预期聚合度量的列字典

print(df)

     timestamp   open     high       low  close
0  2019-11-04 14:23:00  30468  30473.9  30440.15  30445
1  2019-11-04 14:23:00  30468  30473.9  30440.15  30450
2  2019-11-04 14:23:00  30468  30473.9  30440.15  30455
3  2019-11-04 14:23:00  30468  30473.9  30440.15  30460
4  2019-11-04 14:23:00  30468  30479.0  30440.15  30479

df1 = df.groupby(df['timestamp']).agg
           ({'open' : 'first', 'high' : 'max', 'low' : 'min', 'close' : 'last'})
print(df1)
                          open     high       low  close
timestamp                                           
2019-11-04 14:23:00  30468  30479.0  30440.15  30479
如果你不想忽视时间而只关注日期,你可以这样做

df.groupby(df['timestamp'].dt.date).agg
           ({'open' : 'first', 'high' : 'max', 'low' : 'min', 'close' : 'last'})

您可以使用
.agg
并在初始
groupby

print(df)

     timestamp   open     high       low  close
0  2019-11-04 14:23:00  30468  30473.9  30440.15  30445
1  2019-11-04 14:23:00  30468  30473.9  30440.15  30450
2  2019-11-04 14:23:00  30468  30473.9  30440.15  30455
3  2019-11-04 14:23:00  30468  30473.9  30440.15  30460
4  2019-11-04 14:23:00  30468  30479.0  30440.15  30479

df1 = df.groupby(df['timestamp']).agg
           ({'open' : 'first', 'high' : 'max', 'low' : 'min', 'close' : 'last'})
print(df1)
                          open     high       low  close
timestamp                                           
2019-11-04 14:23:00  30468  30479.0  30440.15  30479
如果你不想忽视时间而只关注日期,你可以这样做

df.groupby(df['timestamp'].dt.date).agg
           ({'open' : 'first', 'high' : 'max', 'low' : 'min', 'close' : 'last'})

df.unstack()
df.stack()
?如何确定每个时间戳的“正确”打开、高、低、关闭?Chris如果时间相同,则最高的
将为高。最低下限将是下限,关闭将是最后一行关闭值。“打开”将是第一行打开值。@ansev感谢您的建议。我现在正在读关于
df.unstack()
df.stack()
的书。可能类似于
df.groupby(df['timestamp']).agg({'open':'first','high':'max','low':'min','close':'last'})
df.unstack()
df.stack()
?如何确定“正确”的打开、高、低,按时间戳关闭?@Chris如果时间相同,最高的
high
将为high。最低下限将是下限,关闭将是最后一行关闭值。“打开”将是第一行打开值。@ansev感谢您的建议。我现在正在阅读关于
df.unstack()
df.stack()
的内容。可能类似于
df.groupby(df['timestamp']).agg({'open':'first','high':'max','low':'min','close':'last'})