{"id":1048,"hash":"c5cb8150cc3725f920c8209d37a0df304f51d93db7bf68dad0fe8f1302e012ef","pattern":"LabelEncoder: TypeError: &#39;&gt;&#39; not supported between instances of &#39;float&#39; and &#39;str&#39;","full_message":"I'm facing this error for multiple variables even treating missing values.\nFor example:\n\nle = preprocessing.LabelEncoder()\ncategorical = list(df.select_dtypes(include=['object']).columns.values)\nfor cat in categorical:\n    print(cat)\n    df[cat].fillna('UNK', inplace=True)\n    df[cat] = le.fit_transform(df[cat])\n#     print(le.classes_)\n#     print(le.transform(le.classes_))\n\n---------------------------------------------------------------------------\nTypeError                                 Traceback (most recent call last)\n<ipython-input-24-424a0952f9d0> in <module>()\n      4     print(cat)\n      5     df[cat].fillna('UNK', inplace=True)\n----> 6     df[cat] = le.fit_transform(df[cat].fillna('UNK'))\n      7 #     print(le.classes_)\n      8 #     print(le.transform(le.classes_))\n\nC:\\Users\\paula.ceccon.ribeiro\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\label.py in fit_transform(self, y)\n    129         y = column_or_1d(y, warn=True)\n    130         _check_numpy_unicode_bug(y)\n--> 131         self.classes_, y = np.unique(y, return_inverse=True)\n    132         return y\n    133 \n\nC:\\Users\\paula.ceccon.ribeiro\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\numpy\\lib\\arraysetops.py in unique(ar, return_index, return_inverse, return_counts)\n    209 \n    210     if optional_indices:\n--> 211         perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')\n    212         aux = ar[perm]\n    213     else:\n\nTypeError: '>' not supported between instances of 'float' and 'str'\n\nChecking the variable that lead to the error results ins:\n\ndf['CRM do Médico'].isnull().sum()\n0\n\nBesides nan values, what could be causing this error?","ecosystem":"pypi","package_name":"pandas","package_version":null,"solution":"This is due to the series df[cat] containing elements that have varying data types e.g.(strings and/or floats).  This could be due to the way the data is read, i.e. numbers are read as float and text as strings or the datatype was float and changed after the fillna operation.\n\nIn other words \n\n  pandas data type 'Object' indicates mixed types rather than str type\n\nso using the following line:\n\ndf[cat] = le.fit_transform(df[cat].astype(str))\n\nshould help","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/46406720/labelencoder-typeerror-not-supported-between-instances-of-float-and-str","votes":99,"created_at":"2026-04-19T04:52:13.803624+00:00","updated_at":"2026-04-19T04:52:13.803624+00:00"}