Home > Blockchain >  Pandas How to Check If a Numpy Float Value is Greater than 0
Pandas How to Check If a Numpy Float Value is Greater than 0

Time:11-19

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

CodePudding user response:

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()
  • Related