Skip to content

Commit 6b37b34

Browse files
committed
Implement code as suggested by @jameslamb
1 parent e61bcbe commit 6b37b34

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

python-package/lightgbm/basic.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import scipy.sparse
2828

2929
from .compat import (
30+
CFFI_INSTALLED,
3031
PANDAS_INSTALLED,
3132
PYARROW_INSTALLED,
3233
arrow_cffi,
@@ -1706,8 +1707,8 @@ def __pred_for_pyarrow_table(
17061707
predict_type: int,
17071708
) -> Tuple[np.ndarray, int]:
17081709
"""Predict for a PyArrow table."""
1709-
if not PYARROW_INSTALLED:
1710-
raise LightGBMError("Cannot predict from Arrow without `pyarrow` installed.")
1710+
if not (PYARROW_INSTALLED and CFFI_INSTALLED):
1711+
raise LightGBMError("Cannot predict from Arrow without `pyarrow` and `cffi` installed.")
17111712

17121713
# Check that the input is valid: we only handle numbers (for now)
17131714
if not all(arrow_is_integer(t) or arrow_is_floating(t) or arrow_is_boolean(t) for t in table.schema.types):
@@ -2186,6 +2187,8 @@ def _lazy_init(
21862187
elif isinstance(data, np.ndarray):
21872188
self.__init_from_np2d(data, params_str, ref_dataset)
21882189
elif _is_pyarrow_table(data):
2190+
if not CFFI_INSTALLED:
2191+
raise LightGBMError("Cannot init dataframe from Arrow without `pyarrow` and `cffi` installed.")
21892192
self.__init_from_pyarrow_table(data, params_str, ref_dataset)
21902193
elif isinstance(data, list) and len(data) > 0:
21912194
if _is_list_of_numpy_arrays(data):
@@ -2459,8 +2462,8 @@ def __init_from_pyarrow_table(
24592462
ref_dataset: Optional[_DatasetHandle],
24602463
) -> "Dataset":
24612464
"""Initialize data from a PyArrow table."""
2462-
if not PYARROW_INSTALLED:
2463-
raise LightGBMError("Cannot init dataframe from Arrow without `pyarrow` installed.")
2465+
if not (PYARROW_INSTALLED and CFFI_INSTALLED):
2466+
raise LightGBMError("Cannot init dataframe from Arrow without `pyarrow` and `cffi` installed.")
24642467

24652468
# Check that the input is valid: we only handle numbers (for now)
24662469
if not all(arrow_is_integer(t) or arrow_is_floating(t) or arrow_is_boolean(t) for t in table.schema.types):

python-package/lightgbm/compat.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ def __init__(self, *args: Any, **kwargs: Any):
289289
from pyarrow import ChunkedArray as pa_ChunkedArray
290290
from pyarrow import Table as pa_Table
291291
from pyarrow import chunked_array as pa_chunked_array
292-
from pyarrow.cffi import ffi as arrow_cffi
293292
from pyarrow.types import is_boolean as arrow_is_boolean
294293
from pyarrow.types import is_floating as arrow_is_floating
295294
from pyarrow.types import is_integer as arrow_is_integer
@@ -316,17 +315,6 @@ class pa_Table: # type: ignore
316315
def __init__(self, *args: Any, **kwargs: Any):
317316
pass
318317

319-
class arrow_cffi: # type: ignore
320-
"""Dummy class for pyarrow.cffi.ffi."""
321-
322-
CData = None
323-
addressof = None
324-
cast = None
325-
new = None
326-
327-
def __init__(self, *args: Any, **kwargs: Any):
328-
pass
329-
330318
class pa_compute: # type: ignore
331319
"""Dummy class for pyarrow.compute."""
332320

@@ -338,6 +326,27 @@ class pa_compute: # type: ignore
338326
arrow_is_integer = None
339327
arrow_is_floating = None
340328

329+
330+
"""cffi"""
331+
try:
332+
from pyarrow.cffi import ffi as arrow_cffi
333+
334+
CFFI_INSTALLED = True
335+
except ImportError:
336+
CFFI_INSTALLED = False
337+
338+
class arrow_cffi: # type: ignore
339+
"""Dummy class for pyarrow.cffi.ffi."""
340+
341+
CData = None
342+
addressof = None
343+
cast = None
344+
new = None
345+
346+
def __init__(self, *args: Any, **kwargs: Any):
347+
pass
348+
349+
341350
"""cpu_count()"""
342351
try:
343352
from joblib import cpu_count

0 commit comments

Comments
 (0)