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

Python beginner. I don't know how to solve this error occurred with the code below:TypeError: '>' not supported between instances of 'str' and 'float'

I’m following the tutorial on YouTube called Python programming for Finance. I’ve checked multiple times if I’ve wrote the same code of the YouTuber but I couldn’t find any error on the transcription. Could you help me in finding the error?

def buy_sell_hold(*args): 
    cols = [c for c in args]
    requirement = 0.02
    for col in cols:
        if col > requirement:
            return 1
        if col < -requirement:
            return -1
    return 0
   
    


def extract_featuresets(ticker):
    tickers, df = process_data_for_labels(ticker)
    
#    df['{}_target'.format(ticker)] = list(map(buy_sell_hold,df[[c for c in df.columns if c not in tickers]].values))
    df['{}_target'.format(ticker)] = list(map(buy_sell_hold, 
                                              df['{}_1d'.format(ticker)],
                                              df['{}_2d'.format(ticker)],
                                              df['{}_3d'.format(ticker)],
                                              df['{}_4d'.format(ticker)],
                                              df['{}_5d'.format(ticker)],
                                              df['{}_6d'.format(ticker)],
                                              df['{}_7d'.format(ticker)]))
    vals = df['{}_target'.format(ticker)].values.tolist()
    str_vals = [str(i) for i in vals]
    print('Data spread:', Counter(str_vals))  
    
    df.fillna(0,inplace=True)
    
    df=df.replace([np.inf,-np.inf], np.nan)
    df.dropna(inplace=True)
    
    df_vals = df[[ticker for ticker in tickers]].pct_change()
    df_vals = df_vals.replace([np.inf,-np.inf], 0)
    df_vals.fillna(0,inplace=True)
    
    X = df_vals.values
    Y = df['{}_target'.format(ticker)].values
    
    return X,Y, df

extract_featuresets('AAPL')

this is the error if col > requirement:

TypeError: ‘>’ not supported between instances of ‘str’ and ‘float’

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

>Solution :

Well your doing a comparison between two differents types : A string and a Float.

You can convert the string variable using :

number = float(string)

So i your case :

def buy_sell_hold(*args): 
    cols = [c for c in args]
    requirement = 0.02
    for col in cols:
        if float(col) > requirement:
            return 1
        if float(col) < -requirement:
            return -1
    return 0

But make sure that the args only contains float or add a try catch around the conditions.

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