[英]weird behaviour when executing python script
I don't know if any of you have ever had such a behaviour, but after debugging my code I noticed a really weird thing happening to my code. 我不知道你们中的任何人是否曾经有过这种行为,但是在调试我的代码后,我注意到我的代码发生了一件非常奇怪的事情。 Basically, I am filling a numpy array ('vols') with values contained in a pandas dataframe (with 2 hierarchical columns) at a certain index ('date'), but if I try to execute this piece of code in a script:
基本上,我用一个特定索引(“日期”)的pandas数据框(带有2个层次列)中包含的值填充一个numpy数组(“ vols”),但是如果我尝试在脚本中执行以下代码:
for i in range(capEndDates_size):
for j in range(strike_size):
vols[i,j] = float(df[capEndDates[i]][strikes_list[j]].ix[date])
I cannot properly fill all values of variable 'vols' with those included in the dataframe at index 'date', instead getting some 'nan' where I would expect values. 我无法用索引“日期”中包含在数据框中的变量正确填充变量“ vols”的所有值,而是在我期望值的位置获得一些“ nan”。 The funny thing is that if I execute the piece of code in the interpreter, this goes the right way (ie by pressing f9 on the editor of Spyder, which is the IDE I'm using)!
有趣的是,如果我在解释器中执行这段代码,这是正确的方法(即,在我正在使用的IDE的Spyder编辑器上按f9键)! I found a workaround at this bug by repeating those lines twice (that is, forcing the script to execute the lines two times).
我通过重复两次这些行(即,强制脚本执行两次该行)找到了解决此错误的方法。 My solution is like this:
我的解决方案是这样的:
for i in range(capEndDates_size):
for j in range(strike_size):
vols[i,j] = float(df[capEndDates[i]][strikes_list[j]].ix[date])
for i in range(capEndDates_size):
for j in range(strike_size):
vols[i,j] = float(df[capEndDates[i]][strikes_list[j]].ix[date])
Which is really unacceptable. 这真的是不能接受的。 Does any one have an idea of why this is happening?
有谁知道为什么会这样吗?
如果vols[i,j]
是NaN,为什么不尝试在分配给vols
之后插入硬断点( import pdb; pdb.set_trace()
) vols[i,j]
然后进行调试?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.