gpt4 book ai didi

python 由于多次重复调用 pandas 而卡住

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:27 25 4
gpt4 key购买 nike

我正在尝试计算许多采样频率的时间序列的方差(所谓的签名图),我使用了在一组频率上循环的重采样方法,但 python 在完成任务之前停止了(没有错误,只是卡住)。代码如下

var_list = [timeseries.resample(rule=str(int(freq))+'min',how='first').var() for i in np.linspace(2,20,10)]

请注意,只要少进行一次迭代,一切就都很好(除了内存和 CPU 使用率非常低之外,我不明白为什么再进行一次迭代就不会成功)

[编辑] http://www.filedropper.com/14081

import pandas as pd

data = pd.io.parsers.read_csv(filepath_or_buffer="/media/snake91/01D05438A403F9501/Econometrics/"+datatype+"/"+csvinfile+".csv", sep=',',
decimal='.',usecols=["Date","Time","Close"], keep_date_col=True)
data['DateTime'] = data.apply(lambda row: datetime.datetime.strptime(row['Date']+ ' ' + row['Time'], '%d/%m/%Y %H:%M'), axis=1)
data.set_index('DateTime', inplace=True)

price = data["Close"]

我只使用“关闭”列

[编辑2]经过不断尝试,我得到了这个

*** Error in `/usr/bin/python': double free or corruption (out): 0x00000000030ba810 ***

有错误吗?

最佳答案

你的方法看起来有点复杂......我希望我的简化是你所需要的......

# get an index of pandas Timestamps
df.index = pd.to_datetime(df.Date + ' ' + df.Time)

# get the column we want as a pandas Series called price
price = df['Close']

更新

# use a list comprehension to construct a list of variances, 
# for the various resampling periods
var_list = [price.resample(str(int(i))+'min', how='first').var()
for i in np.linspace(2,20,10)]

这产生了......

In [10]: var_list
Out[10]:
[0.077889810612269461,
0.077385129726302446,
0.079956521234607447,
0.077604408646643086,
0.077813415563354235,
0.080675086585717218,
0.074652971598985707,
0.0763870569776786,
0.076195162549351256,
0.076852363707017035]

以字典形式...

In [11]: %paste
# use a comprehension to construct a dictionary, each entry of which
# has the variance for each resampling period
var_dic = {i: price.resample(str(int(i))+'min', how='first').var()
for i in np.linspace(2,20,10)}
## -- End pasted text --

In [12]: var_dic
Out[12]:
{2.0: 0.077889810612269461,
4.0: 0.077385129726302446,
6.0: 0.079956521234607447,
8.0: 0.077604408646643086,
10.0: 0.077813415563354235,
12.0: 0.080675086585717218,
14.0: 0.074652971598985707,
16.0: 0.0763870569776786,
18.0: 0.076195162549351256,
20.0: 0.076852363707017035}

关于python 由于多次重复调用 pandas 而卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29876309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com