我将此计算出的Pandas数据帧作为df,具有浮动值,字符串索引和nan值。我的逻辑很简单,在特定列中尝试检查最后一个元素值是(==> = <=下面给出一个示例)
在这里抛出了IF条件错误并终止了执行,为什么会发生这种情况。如何解决此问题?南难道是问题所在吗?
Time close ST_100_0.885 ST_Test
---- 0.134 1.1111 nan
---- 1.258 2.2222 dn
---- 3.255 3.1212 up
---- 4.256 4.3232 up
---- 4.356 5.4343 dn
import requests
import time
import pandas as pd
import json
from pandas.io.json import json_normalize
df=df.fillna(0)
if (df['ST_100_0.885'][-1] <= df['close'][-1]):
print("test passed")
我所做的一些检查表明df具有预期的对象类型float值
我在调试时遇到的错误
if (df['ST_100_0.885'][-1] <= df['close'][-1]):
in __getitem__
result = self.index.get_value(self, key)
in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4363)
File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4046)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)
File "pandas\_libs\hashtable_class_helper.pxi", line 756, in pandas._libs.hashtable.Int64HashTable.get_item (pandas\_libs\hashtable.c:13913)
File "pandas\_libs\hashtable_class_helper.pxi", line 762, in pandas._libs.hashtable.Int64HashTable.get_item (pandas\_libs\hashtable.c:13857)
KeyError: -1
close float64
high float64
low float64
open float64
time datetime64[ns, US/Alaska]
volumefrom float64
volumeto float64
dtype: objec
t
请注意,[-1]用于访问最后一个元素,我还需要在我的逻辑中访问前一个元素到最后一个元素,如果这导致了如何正确访问前一个元素和最后一个元素的问题。
答案 0 :(得分:0)
您可以使用尾巴
df=df.fillna(0)
if (df['ST_100_0.885'].tail(1) <= df['close'].tail(1)):
print("test passed")
“编辑”
df=df.fillna(0)
if (df['ST_100_0.885'][df.index[-1]] >= df['close'][df.index[-1]]):
print("test passed")
退出:
test passed
您也可以尝试使用iget