Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Python3 and I'm working on several files where some of my data (AYield & BYield) is missing which is considered a NaN, however, when I'm running the last line of the code, I get an error. Both Ask and Bid data frames contain the same rows and columns. Thank you

Askyield = pd.read_excel("AYield.xlsx",na_values=["NaN"])
Bidyield = pd.read_excel("BYield.xlsx",na_value=["NaN"])
matchedbond_info = pd.read_excel("matched_bonds.xlsx")

Askyield = pd.merge(matchedbond_info, Askyield, on = ['ISIN'])
Bidyield = pd.merge(matchedbond_info, Bidyield, on = ['ISIN'])

date_list = []                       
for i in range(len(Bidyield.columns)):
    if isinstance(Bidyield.columns[i], dt.datetime):date_list.append(Bidyield.columns[i])
    
matchedbond_info = Bidyield.drop(columns=date_list)

bid_yield.info()

bid_yield.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1236 entries, 0 to 1235
Columns: 1566 entries, 2019-12-31 00:00:00 to 2014-01-01 00:00:00
dtypes: float64(1566)

bid_yield = Bidyield[date_list]
ask_yield = Askyield[date_list]

bid_yield.head()

   2019-12-31  2019-12-30  2019-12-27  ...  2014-01-03  2014-01-02  2014-01-01
0         NaN         NaN         NaN  ...         NaN         NaN         NaN
1       3.119       3.084       3.081  ...         NaN         NaN         NaN
2         NaN         NaN         NaN  ...         NaN         NaN         NaN
3         NaN         NaN         NaN  ...         NaN         NaN         NaN
4         NaN         NaN         NaN  ...         NaN         NaN         NaN

[5 rows x 1566 columns]

bid_yield = bid_yield.mask((bid_yield >0) & (ask_yield <0))

Then I get the following

    TypeError: '<' not supported between instances of 'str' and 'int'
like image 930
Sbjoo25 Avatar asked Oct 27 '25 14:10

Sbjoo25


1 Answers

I can reproduce this error with this example:

import pandas as pd

df = pd.DataFrame(dict(x=["5", "10"], 
                       y=[1, 4]))

df.dtypes
# x    object
# y     int64
# dtype: object

df[df.x > df.y]

# TypeError: '>' not supported between instances of 'str' and 'int'

You probably need to convert one of the columns to float.

In this example:

df['x'] = df.x.astype("float")
like image 152
marbel Avatar answered Oct 30 '25 07:10

marbel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!