0

I have this computed Pandas dataframe as df which has floating value, string index, and nan values. My logic is very simple that in a particular column trying to check last element value is (== >= <= one example given below)

Here in the IF condition error thrown and the execution terminated ,Why is this happening.How to resolve this issue ? Does Nan cause the problem?

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")

some of the checks i have done that shows the df has the object type float value which is as expected

Errors that i am getting while debugging

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

Note that [-1] is used to access last element ,i also need to access previous to last element in my logic ,if this causing the problem how to access last and previous to last element correctly.

Marx Babu
  • 750
  • 3
  • 11
  • 34

1 Answers1

0

you can use tail

df=df.fillna(0)
if (df['ST_100_0.885'].tail(1) <= df['close'].tail(1)):
   print("test passed")

'edit'

df=df.fillna(0)
if (df['ST_100_0.885'][df.index[-1]] >= df['close'][df.index[-1]]):
    print("test passed")

out:

test passed

You can also try of using iget

Naga kiran
  • 4,528
  • 1
  • 17
  • 31