(
data: DataType,
feature_names: Optional[FeatureNames],
feature_types: Optional[Union[FeatureTypes, Categories]],
enable_categorical: bool,
)
| 1072 | |
| 1073 | |
| 1074 | def _transform_cudf_df( |
| 1075 | data: DataType, |
| 1076 | feature_names: Optional[FeatureNames], |
| 1077 | feature_types: Optional[Union[FeatureTypes, Categories]], |
| 1078 | enable_categorical: bool, |
| 1079 | ) -> Tuple[ |
| 1080 | CudfTransformed, |
| 1081 | Optional[FeatureNames], |
| 1082 | Optional[FeatureTypes], |
| 1083 | ]: |
| 1084 | is_bool_dtype = _lazy_load_cudf_is_bool() |
| 1085 | |
| 1086 | is_categorical_dtype = _lazy_load_cudf_is_cat() |
| 1087 | # Work around https://github.com/dmlc/xgboost/issues/10181 |
| 1088 | if _is_cudf_ser(data): |
| 1089 | if is_bool_dtype(data.dtype): |
| 1090 | data = data.astype(np.uint8) |
| 1091 | dtypes = [data.dtype] |
| 1092 | else: |
| 1093 | data = data.astype( |
| 1094 | {col: np.uint8 for col in data.select_dtypes(include="bool")} |
| 1095 | ) |
| 1096 | dtypes = data.dtypes |
| 1097 | |
| 1098 | if not all( |
| 1099 | dtype.name in _pandas_dtype_mapper |
| 1100 | or (is_categorical_dtype(dtype) and enable_categorical) |
| 1101 | for dtype in dtypes |
| 1102 | ): |
| 1103 | _invalid_dataframe_dtype(data) |
| 1104 | |
| 1105 | # handle feature names |
| 1106 | if feature_names is None: |
| 1107 | if _is_cudf_ser(data): |
| 1108 | feature_names = [data.name] |
| 1109 | elif lazy_isinstance(data.columns, "cudf.core.multiindex", "MultiIndex"): |
| 1110 | feature_names = [" ".join([str(x) for x in i]) for i in data.columns] |
| 1111 | else: |
| 1112 | feature_names = list(data.columns.map(str)) |
| 1113 | |
| 1114 | # handle feature types |
| 1115 | feature_types, ref_categories = get_ref_categories(feature_types) |
| 1116 | if feature_types is None: |
| 1117 | feature_types = [] |
| 1118 | for dtype in dtypes: |
| 1119 | if is_categorical_dtype(dtype) and enable_categorical: |
| 1120 | feature_types.append(CAT_T) |
| 1121 | else: |
| 1122 | feature_types.append(_pandas_dtype_mapper[dtype.name]) |
| 1123 | |
| 1124 | # handle categorical data |
| 1125 | result = [] |
| 1126 | if _is_cudf_ser(data): |
| 1127 | # unlike pandas, cuDF uses NA for missing data. |
| 1128 | if is_categorical_dtype(data.dtype) and enable_categorical: |
| 1129 | result.append(data.cat) |
| 1130 | elif is_categorical_dtype(data.dtype) and not enable_categorical: |
| 1131 | raise ValueError(_ENABLE_CAT_ERR) |
no test coverage detected