Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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重新采样olhc数据帧时出错_Python_Pandas_Dataframe - Fatal编程技术网

使用python重新采样olhc数据帧时出错

使用python重新采样olhc数据帧时出错,python,pandas,dataframe,Python,Pandas,Dataframe,当尝试将OHLC数据帧从1m重新采样到每小时一次时,我遇到以下错误: 数据帧 df.info() # Column Dtype --- ------ ----- 0 Date_Time datetime64[ns] 1 Open float64 2 High float64 3 Low float64 4 Close

当尝试将OHLC数据帧从1m重新采样到每小时一次时,我遇到以下错误:

数据帧

df.info()

#   Column     Dtype         
---  ------     -----         
 0   Date_Time  datetime64[ns]
 1   Open       float64       
 2   High       float64       
 3   Low        float64       
 4   Close      float64

df.tail()

            Date_Time            Open    High    Low     Close
    1692259 2014-12-30 20:51:00  2321.0  1213.0  1223.0  2334.0
    1692260 2014-12-30 20:52:00  2342.0  2322.0  2332.0  2332.0
    1692261 2014-12-30 20:53:00  3421.0  2322.0  2334.0  2123.0
    1692262 2014-12-30 20:54:00  2312.0  2332.0  2324.0  2321.0
    1692263 2014-12-30 20:55:00  2312.0  1212.0  2343.0  2323.0
...
尝试1

df_ohlc = df.resample('60T', on='Date_Time').ohlc()
错误

DataError: No numeric types to aggregate
尝试2

使用venky_uu_uu推荐给另一个具有类似解决方案的帖子

df_ohlc = df.resample('60T', on='Date_Time').agg({
    'Open':'first',
    'High':'max',
    'Low':'min',
    'Close':'last'
})
NaN包括在内,但df是干净的。如何避免这种情况?

                       Open    High     Low   Close
Date_Time                                          
2015-12-26 18:00:00     NaN     NaN     NaN     NaN
2015-12-26 19:00:00     NaN     NaN     NaN     NaN
2015-12-26 20:00:00     NaN     NaN     NaN     NaN
2015-12-26 21:00:00     NaN     NaN     NaN     NaN
2015-12-26 22:00:00     NaN     NaN     NaN     NaN

下一个版本的代码适合我,由于您提供的行很少,我只做了2分钟的聚合

我认为不工作的原因是因为您的数据是字符串格式的,而不是需要日期时间列是日期时间格式的,其余的是浮点数格式的,而不是字符串。我在代码中正确地从字符串转换为日期时间

或者另一个不工作的原因是因为您没有执行
df.set\u index('Date\u Time')
,我在代码中也这样做了

输出为:

            Date_Time    Open    High     Low   Close
0 2014-12-30 20:51:00  2321.0  1213.0  1223.0  2334.0
1 2014-12-30 20:52:00  2342.0  2322.0  2332.0  2332.0
2 2014-12-30 20:53:00  3421.0  2322.0  2334.0  2123.0
3 2014-12-30 20:54:00  2312.0  2332.0  2324.0  2321.0
4 2014-12-30 20:55:00  2312.0  1212.0  2343.0  2323.0
                       Open    High     Low   Close
Date_Time
2014-12-30 20:50:00  2321.0  1213.0  1223.0  2334.0
2014-12-30 20:52:00  2342.0  2322.0  2332.0  2123.0
2014-12-30 20:54:00  2312.0  2332.0  2324.0  2323.0

这回答了你的问题吗?谢谢@venky_uu;是和否,出现了NAs值,但数据帧是干净的。更新的问题。非常感谢@Arty。如果将Date\u time设置为索引,则会起作用,我认为使用on='Date\u time'参数不需要Date\u time作为索引。但不管怎样,现在是不是很好
            Date_Time    Open    High     Low   Close
0 2014-12-30 20:51:00  2321.0  1213.0  1223.0  2334.0
1 2014-12-30 20:52:00  2342.0  2322.0  2332.0  2332.0
2 2014-12-30 20:53:00  3421.0  2322.0  2334.0  2123.0
3 2014-12-30 20:54:00  2312.0  2332.0  2324.0  2321.0
4 2014-12-30 20:55:00  2312.0  1212.0  2343.0  2323.0
                       Open    High     Low   Close
Date_Time
2014-12-30 20:50:00  2321.0  1213.0  1223.0  2334.0
2014-12-30 20:52:00  2342.0  2322.0  2332.0  2123.0
2014-12-30 20:54:00  2312.0  2332.0  2324.0  2323.0