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:
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. 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)! 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]
然后进行调试?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.