xgboost error: dtypes must be int, float or bool, but they are

Viewed 621

I have a preprocessed real estate related DataFrame with the following data types:

>> df.dtype

OHE_Cat__x0_Single Family Residential      Int64
OHE_Cat__x0_Townhouse                      Int64
OHE_Cat__x1_1                              Int64
OHE_Cat__x1_2                              Int64
ZIP OR POSTAL CODE                         Int64
PRICE                                      Int64
BEDS                                       Int64
BATHS                                    Float64
SQUARE FEET                                Int64
LOT SIZE                                   Int64
YEAR BUILT                                 Int64
LATITUDE                                 Float64
LONGITUDE                                Float64
Age                                        Int64
TIME VAR UNIX                              Int64
is_Pandemic                              boolean
is_CY                                    boolean
dtype: object

However when trying to fit my XGBRegressor, I get the following error:

ValueError: DataFrame.dtypes for data must be int, float or bool.
            Did not expect the data types in fields OHE_Cat__x0_Single Family Residential, OHE_Cat__x0_Townhouse, OHE_Cat__x1_1, OHE_Cat__x1_2, ZIP OR POSTAL CODE, BEDS, BATHS, SQUARE FEET, LOT SIZE, YEAR BUILT, LATITUDE, LONGITUDE, Age, TIME VAR UNIX, is_Pandemic, is_CY

Oddly, this error was not present when I was using pd.get_dummmies, but after switching to sklearn.preprocessing.OneHotEncoder I started receiving it so I'm thinking there is in an error in my code below?

oneHotE = OneHotEncoder(drop='first')
transformer = ColumnTransformer([('OHE_Cat', oneHotE, categorical)], remainder = 'passthrough')
    
df = pd.DataFrame(transformer.fit_transform(df), columns= transformer.get_feature_names()).convert_dtypes()

I've tried the following alterations to my dataset before a train_test_split:

df = df.apply(pd.to_numeric, axis=1)
df = df.apply(pd.to_numeric, axis=0)
df = df.convert_dtypes()
df = df.dropna()
df = df._get_numeric_data()

EDIT:

  • RandomForestRegressor runs fine without errors, but gives the following warning:

futurewarning: Arrays of bytes/strings is being converted to decimal numbers if dtype='numeric'. This behavior is deprecated in 0.24 and will be removed in 1.1 (renaming of 0.26). Please convert your data to numeric values explicitly instead.

  • CatBoostRegressor gives the following error: Cannot convert FloatingArray to numpy.ndarray
0 Answers
Related