diff --git a/python-package/lightgbm/basic.py b/python-package/lightgbm/basic.py index dfa7b6008718..3da6ddc5d78c 100644 --- a/python-package/lightgbm/basic.py +++ b/python-package/lightgbm/basic.py @@ -2187,8 +2187,6 @@ def _lazy_init( elif isinstance(data, np.ndarray): self.__init_from_np2d(data, params_str, ref_dataset) elif _is_pyarrow_table(data): - if not CFFI_INSTALLED: - raise LightGBMError("Cannot init dataframe from Arrow without `pyarrow` and `cffi` installed.") self.__init_from_pyarrow_table(data, params_str, ref_dataset) elif isinstance(data, list) and len(data) > 0: if _is_list_of_numpy_arrays(data): diff --git a/tests/python_package_test/test_arrow.py b/tests/python_package_test/test_arrow.py index d8246f3842de..a520b9941afe 100644 --- a/tests/python_package_test/test_arrow.py +++ b/tests/python_package_test/test_arrow.py @@ -1,8 +1,10 @@ # coding: utf-8 import filecmp import os +import sys from pathlib import Path from typing import Any, Dict, Optional +from unittest import mock import numpy as np import pytest @@ -454,3 +456,25 @@ def test_arrow_feature_name_manual(): ) booster = lgb.train({"num_leaves": 7}, dataset, num_boost_round=5) assert booster.feature_name() == ["c", "d"] + + +def test_training_and_predicting_from_pa_table_without_cffi_raises(): + data = generate_dummy_arrow_table() + with mock.patch.dict(sys.modules, {"cffi": None}): + assert lgb.compat.PYARROW_INSTALLED is True + + with pytest.raises( + lgb.basic.LightGBMError, match="Cannot init dataframe from Arrow without `pyarrow` and `cffi` installed." + ): + dataset = lgb.Dataset( + data, + label=pa.array([0, 1, 0, 0, 1]), + params=dummy_dataset_params(), + feature_name=["c", "d"], + categorical_feature=["c"], + ) + + with pytest.raises( + lgb.basic.LightGBMError, match="Cannot predict from Arrow without `pyarrow` and `cffi` installed." + ): + _ = lgb.train({"num_leaves": 7}, dataset, num_boost_round=5)