Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for the latest pandas. #10266

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions python-package/xgboost/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,8 @@ def pandas_feature_info(
if feature_names is None and meta is None:
if isinstance(data.columns, pd.MultiIndex):
feature_names = [" ".join([str(x) for x in i]) for i in data.columns]
elif isinstance(data.columns, (pd.Index, pd.RangeIndex)):
feature_names = list(map(str, data.columns))
else:
feature_names = data.columns.format()
feature_names = list(data.columns.map(str))

# handle feature types
if feature_types is None and meta is None:
Expand Down Expand Up @@ -865,18 +863,30 @@ def _is_cudf_df(data: DataType) -> bool:
return lazy_isinstance(data, "cudf.core.dataframe", "DataFrame")


def _get_cudf_cat_predicate() -> Callable[[Any], bool]:
try:
from cudf import CategoricalDtype

def is_categorical_dtype(dtype: Any) -> bool:
return isinstance(dtype, CategoricalDtype)

except ImportError:
try:
from cudf.api.types import is_categorical_dtype # type: ignore
except ImportError:
from cudf.utils.dtypes import is_categorical_dtype # type: ignore

return is_categorical_dtype


def _cudf_array_interfaces(data: DataType, cat_codes: list) -> bytes:
"""Extract CuDF __cuda_array_interface__. This is special as it returns a new list
of data and a list of array interfaces. The data is list of categorical codes that
caller can safely ignore, but have to keep their reference alive until usage of
array interface is finished.

"""
try:
from cudf.api.types import is_categorical_dtype
except ImportError:
from cudf.utils.dtypes import is_categorical_dtype

is_categorical_dtype = _get_cudf_cat_predicate()
interfaces = []

def append(interface: dict) -> None:
Expand Down Expand Up @@ -908,12 +918,13 @@ def _transform_cudf_df(
feature_types: Optional[FeatureTypes],
enable_categorical: bool,
) -> Tuple[ctypes.c_void_p, list, Optional[FeatureNames], Optional[FeatureTypes]]:

try:
from cudf.api.types import is_bool_dtype, is_categorical_dtype
from cudf.api.types import is_bool_dtype
except ImportError:
from cudf.utils.dtypes import is_categorical_dtype
from pandas.api.types import is_bool_dtype

is_categorical_dtype = _get_cudf_cat_predicate()
# Work around https://github.com/dmlc/xgboost/issues/10181
if _is_cudf_ser(data):
if is_bool_dtype(data.dtype):
Expand Down Expand Up @@ -941,15 +952,8 @@ def _transform_cudf_df(
feature_names = [data.name]
elif lazy_isinstance(data.columns, "cudf.core.multiindex", "MultiIndex"):
feature_names = [" ".join([str(x) for x in i]) for i in data.columns]
elif (
lazy_isinstance(data.columns, "cudf.core.index", "RangeIndex")
or lazy_isinstance(data.columns, "cudf.core.index", "Int64Index")
# Unique to cuDF, no equivalence in pandas 1.3.3
or lazy_isinstance(data.columns, "cudf.core.index", "Int32Index")
):
feature_names = list(map(str, data.columns))
else:
feature_names = data.columns.format()
feature_names = list(data.columns.map(str))

# handle feature types
if feature_types is None:
Expand Down
24 changes: 14 additions & 10 deletions tests/python/test_with_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,12 @@ def test_pandas_sparse(self):
}
)
y = pd.Series(pd.arrays.SparseArray(np.random.randn(rows)))
dtrain = xgb.DMatrix(X, y)
with pytest.warns(UserWarning, match="Sparse arrays from pandas"):
dtrain = xgb.DMatrix(X, y)
booster = xgb.train({}, dtrain, num_boost_round=4)
predt_sparse = booster.predict(xgb.DMatrix(X))
predt_dense = booster.predict(xgb.DMatrix(X.sparse.to_dense()))
with pytest.warns(UserWarning, match="Sparse arrays from pandas"):
predt_sparse = booster.predict(xgb.DMatrix(X))
predt_dense = booster.predict(xgb.DMatrix(X.sparse.to_dense()))
np.testing.assert_allclose(predt_sparse, predt_dense)

def test_pandas_label(
Expand Down Expand Up @@ -572,14 +574,16 @@ def test_pandas_sparse_column_split(self):
y = pd.Series(pd.arrays.SparseArray(np.random.randn(rows)))

def verify_pandas_sparse():
dtrain = xgb.DMatrix(X, y, data_split_mode=DataSplitMode.COL)
with pytest.warns(UserWarning, match="Sparse arrays from pandas"):
dtrain = xgb.DMatrix(X, y, data_split_mode=DataSplitMode.COL)
booster = xgb.train({}, dtrain, num_boost_round=4)
predt_sparse = booster.predict(
xgb.DMatrix(X, data_split_mode=DataSplitMode.COL)
)
predt_dense = booster.predict(
xgb.DMatrix(X.sparse.to_dense(), data_split_mode=DataSplitMode.COL)
)
with pytest.warns(UserWarning, match="Sparse arrays from pandas"):
predt_sparse = booster.predict(
xgb.DMatrix(X, data_split_mode=DataSplitMode.COL)
)
predt_dense = booster.predict(
xgb.DMatrix(X.sparse.to_dense(), data_split_mode=DataSplitMode.COL)
)
np.testing.assert_allclose(predt_sparse, predt_dense)

tm.run_with_rabit(world_size=3, test_fn=verify_pandas_sparse)
Expand Down
Loading