Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Weird not supported operation error in simple loop over dataframe

I work on a dataframe and I want to iterate over one column as I did this hundreds of times with many dataframes. Today I get an error and I can’t wrap my head around what’s wrong with it.
Maybe worth mentioning, the dataframe is a concatenation.

log = (pd.concat([log_entry,log_exit]).sort_values(by=['date']))

dataframe:

                    position order  price       PnL
date                
2022-03-27 20:45:00 short   entry   29.242291   0
2022-03-28 13:45:00 short   entry   31.052375   0
2022-03-28 15:00:00 short   entry   31.072893   0
2022-03-28 19:15:00 short   entry   31.070073   0
2022-03-28 20:45:00 short   entry   31.220069   0
2022-03-28 23:00:00 -          TP   30.016500   0
2022-03-28 23:15:00 -          TP   29.788000   0
2022-03-28 23:15:00 -          TP   29.820500   0
2022-03-28 23:30:00 -          TP   29.640500   0
2022-03-29 05:30:00 short   entry   30.902677   0
2022-03-29 06:15:00 short   entry   30.893078   0

iteration:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in range(len(log.index)):
    if log.position[i] == 'short':
            print('ok')

error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4729         try:
-> 4730             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4731         except KeyError as e1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine._get_loc_duplicates()

TypeError: '<' not supported between instances of 'str' and 'int'

During handling of the above exception, another exception occurred:

IndexError                                Traceback (most recent call last)
<ipython-input-8-c8832d66a85a> in <module>
    156 
    157 for i in range(len(log.index)):
--> 158     if log.position[i] == 'short':
    159             print('ok')
    160 #         dd_buffer.append(log.price[i])

/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
   1066         key = com.apply_if_callable(key, self)
   1067         try:
-> 1068             result = self.index.get_value(self, key)
   1069 
   1070             if not is_scalar(result):

/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4748             # python 3
   4749             if is_scalar(key):  # pragma: no cover
-> 4750                 raise IndexError(key)
   4751             raise InvalidIndexError(key)
   4752 

IndexError: 0

How is this possible??

>Solution :

With log.position[i] you are matching the index of the dataframe, i.e. the datetimes. There is no index 0 (only datetimes), that’s why you get the IndexError. Use log.position.iloc[i] instead for integer-location based indexing for selection by position.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading