@@ -370,10 +370,8 @@ def pandas_feature_info(
370
370
if feature_names is None and meta is None :
371
371
if isinstance (data .columns , pd .MultiIndex ):
372
372
feature_names = [" " .join ([str (x ) for x in i ]) for i in data .columns ]
373
- elif isinstance (data .columns , (pd .Index , pd .RangeIndex )):
374
- feature_names = list (map (str , data .columns ))
375
373
else :
376
- feature_names = data .columns .format ( )
374
+ feature_names = list ( data .columns .map ( str ) )
377
375
378
376
# handle feature types
379
377
if feature_types is None and meta is None :
@@ -865,18 +863,30 @@ def _is_cudf_df(data: DataType) -> bool:
865
863
return lazy_isinstance (data , "cudf.core.dataframe" , "DataFrame" )
866
864
867
865
866
+ def _get_cudf_cat_predicate () -> Callable [[Any ], bool ]:
867
+ try :
868
+ from cudf import CategoricalDtype
869
+
870
+ def is_categorical_dtype (dtype : Any ) -> bool :
871
+ return isinstance (dtype , CategoricalDtype )
872
+
873
+ except ImportError :
874
+ try :
875
+ from cudf .api .types import is_categorical_dtype # type: ignore
876
+ except ImportError :
877
+ from cudf .utils .dtypes import is_categorical_dtype # type: ignore
878
+
879
+ return is_categorical_dtype
880
+
881
+
868
882
def _cudf_array_interfaces (data : DataType , cat_codes : list ) -> bytes :
869
883
"""Extract CuDF __cuda_array_interface__. This is special as it returns a new list
870
884
of data and a list of array interfaces. The data is list of categorical codes that
871
885
caller can safely ignore, but have to keep their reference alive until usage of
872
886
array interface is finished.
873
887
874
888
"""
875
- try :
876
- from cudf .api .types import is_categorical_dtype
877
- except ImportError :
878
- from cudf .utils .dtypes import is_categorical_dtype
879
-
889
+ is_categorical_dtype = _get_cudf_cat_predicate ()
880
890
interfaces = []
881
891
882
892
def append (interface : dict ) -> None :
@@ -908,12 +918,13 @@ def _transform_cudf_df(
908
918
feature_types : Optional [FeatureTypes ],
909
919
enable_categorical : bool ,
910
920
) -> Tuple [ctypes .c_void_p , list , Optional [FeatureNames ], Optional [FeatureTypes ]]:
921
+
911
922
try :
912
- from cudf .api .types import is_bool_dtype , is_categorical_dtype
923
+ from cudf .api .types import is_bool_dtype
913
924
except ImportError :
914
- from cudf .utils .dtypes import is_categorical_dtype
915
925
from pandas .api .types import is_bool_dtype
916
926
927
+ is_categorical_dtype = _get_cudf_cat_predicate ()
917
928
# Work around https://github.com/dmlc/xgboost/issues/10181
918
929
if _is_cudf_ser (data ):
919
930
if is_bool_dtype (data .dtype ):
@@ -941,15 +952,8 @@ def _transform_cudf_df(
941
952
feature_names = [data .name ]
942
953
elif lazy_isinstance (data .columns , "cudf.core.multiindex" , "MultiIndex" ):
943
954
feature_names = [" " .join ([str (x ) for x in i ]) for i in data .columns ]
944
- elif (
945
- lazy_isinstance (data .columns , "cudf.core.index" , "RangeIndex" )
946
- or lazy_isinstance (data .columns , "cudf.core.index" , "Int64Index" )
947
- # Unique to cuDF, no equivalence in pandas 1.3.3
948
- or lazy_isinstance (data .columns , "cudf.core.index" , "Int32Index" )
949
- ):
950
- feature_names = list (map (str , data .columns ))
951
955
else :
952
- feature_names = data .columns .format ( )
956
+ feature_names = list ( data .columns .map ( str ) )
953
957
954
958
# handle feature types
955
959
if feature_types is None :
0 commit comments