1

So I have a DataFrame called df1 with the following setup:

                                Open    Close         DiffMa    %Percentage  Open   Close   DiffMa2  %Percentage2
2022-11-04 13:30:00-04:00   42.099998   42.224998   -0.135001   0.296912    13.9586 14.0150 -0.06584    0.404054
2022-11-04 14:30:00-04:00   42.220001   42.330002   0.028002    0.260541    14.0150 14.0550  0.01816        0.285408
2022-11-04 15:30:00-04:00   42.320000   42.630001   0.311001    0.732517    14.0600 14.1100  0.07716        0.355613
2022-11-07 09:30:00-05:00   43.049999   42.294998   -0.019002   -1.753777   14.3200 14.0299 -0.00308    -2.025839
2022-11-07 10:30:00-05:00   42.299999   42.195000   -0.140000   -0.248226   14.0300 13.9865 -0.05278    -0.310050


df1['%Percentage'][0]
Out[9]:
0.2969121247755807

Now I want to check how often %Percentage and %Percentage2 are both positive / both negative for this I created a simple loop however I am continuously running into a key error.

To be specific I shortened down the loop to this:

countera = 0
counterb = 0

for i in df1['%Percentage']:
    if df1['%Percentage'][i] > 0:
       countera = 0

However when I run this loop I get the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-cc9f6f8fa7d5> in <module>
      4 
      5 for i in df1['%Percentage']:
----> 6     if df1['%Percentage'][i] > 0:
      7         countera += 1
      8 

~/opt/anaconda3/envs/StockPredictionGameification/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    880 
    881         elif key_is_scalar:
--> 882             return self._get_value(key)
    883 
    884         if is_hashable(key):

~/opt/anaconda3/envs/StockPredictionGameification/lib/python3.6/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
    988 
    989         # Similar to Index.get_value, but we do not fall back to positional
--> 990         loc = self.index.get_loc(label)
    991         return self.index._get_values_for_loc(self, loc, label)
    992 

~/opt/anaconda3/envs/StockPredictionGameification/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
    620         else:
    621             # unrecognized type
--> 622             raise KeyError(key)
    623 
    624         try:

KeyError: 0.2969121247755807

Now I am trying to figure out why this happens. My guess is that Python somehow can't read the farthest decimal places and assumes it as a different number however I cant seem to find anything specific only as to why this would happen

5
  • No, your issue is due to trying to use the values as indices. See my answer for a vectorial method to achieve what you want. Commented Nov 18, 2022 at 14:02
  • So just for my better understanding what do you mean when you say that I am trying to use the values as indices ? Do you mean that this part:: for i in df1['%Percentage']: is wrong for using the %percentage values as an index for looping through with I or am I completely off ? Commented Nov 18, 2022 at 14:05
  • You would have needed for idx, val in df1['%Percentage'].items(): Commented Nov 18, 2022 at 14:12
  • That does make more sense. So could I also have used as an example for i, len in enumerate df1['%Percentage'] ? Where I would just count the length to get I Commented Nov 18, 2022 at 15:33
  • Honestly, using a loop for that would be a pandas anti-pattern ;) Commented Nov 18, 2022 at 15:35

1 Answer 1

1

To count the number of times in which %Percentage and %Percentage2 have the same sign, use:

import numpy as np
count = np.sign(df1['%Percentage']).eq(np.sign(df1['%Percentage2'])).sum()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.