Skip to content

Commit e7b23d4

Browse files
authored
ERR: Add NumbaUtilError (#33816)
1 parent 63651f3 commit e7b23d4

File tree

8 files changed

+31
-35
lines changed

8 files changed

+31
-35
lines changed

doc/source/reference/general_utility_functions.rst

+3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ Exceptions and warnings
3535
.. autosummary::
3636
:toctree: api/
3737

38+
errors.AccessorRegistrationWarning
3839
errors.DtypeWarning
3940
errors.EmptyDataError
4041
errors.OutOfBoundsDatetime
42+
errors.MergeError
43+
errors.NumbaUtilError
4144
errors.ParserError
4245
errors.ParserWarning
4346
errors.PerformanceWarning

pandas/core/groupby/generic.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
NUMBA_FUNC_CACHE,
8080
check_kwargs_and_nopython,
8181
get_jit_arguments,
82-
is_numba_util_related_error,
8382
jit_user_function,
8483
split_for_numba,
8584
validate_udf,
@@ -283,10 +282,8 @@ def aggregate(
283282
return self._python_agg_general(
284283
func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs
285284
)
286-
except (ValueError, KeyError) as err:
285+
except (ValueError, KeyError):
287286
# Do not catch Numba errors here, we want to raise and not fall back.
288-
if is_numba_util_related_error(str(err)):
289-
raise err
290287
# TODO: KeyError is raised in _python_agg_general,
291288
# see see test_groupby.test_basic
292289
result = self._aggregate_named(func, *args, **kwargs)

pandas/core/util/numba_.py

+8-22
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,11 @@
88

99
from pandas._typing import FrameOrSeries
1010
from pandas.compat._optional import import_optional_dependency
11+
from pandas.errors import NumbaUtilError
1112

1213
NUMBA_FUNC_CACHE: Dict[Tuple[Callable, str], Callable] = dict()
1314

1415

15-
def is_numba_util_related_error(err_message: str) -> bool:
16-
"""
17-
Check if an error was raised from one of the numba utility functions
18-
19-
For cases where a try/except block has mistakenly caught the error
20-
and we want to re-raise
21-
22-
Parameters
23-
----------
24-
err_message : str,
25-
exception error message
26-
27-
Returns
28-
-------
29-
bool
30-
"""
31-
return "The first" in err_message or "numba does not" in err_message
32-
33-
3416
def check_kwargs_and_nopython(
3517
kwargs: Optional[Dict] = None, nopython: Optional[bool] = None
3618
) -> None:
@@ -51,10 +33,10 @@ def check_kwargs_and_nopython(
5133
5234
Raises
5335
------
54-
ValueError
36+
NumbaUtilError
5537
"""
5638
if kwargs and nopython:
57-
raise ValueError(
39+
raise NumbaUtilError(
5840
"numba does not support kwargs with nopython=True: "
5941
"https://github.com/numba/numba/issues/2916"
6042
)
@@ -169,6 +151,10 @@ def f(values, index, ...):
169151
Returns
170152
-------
171153
None
154+
155+
Raises
156+
------
157+
NumbaUtilError
172158
"""
173159
udf_signature = list(inspect.signature(func).parameters.keys())
174160
expected_args = ["values", "index"]
@@ -177,7 +163,7 @@ def f(values, index, ...):
177163
len(udf_signature) < min_number_args
178164
or udf_signature[:min_number_args] != expected_args
179165
):
180-
raise ValueError(
166+
raise NumbaUtilError(
181167
f"The first {min_number_args} arguments to {func.__name__} must be "
182168
f"{expected_args}"
183169
)

pandas/errors/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,9 @@ def __str__(self) -> str:
184184
else:
185185
name = type(self.class_instance).__name__
186186
return f"This {self.methodtype} must be defined in the concrete class {name}"
187+
188+
189+
class NumbaUtilError(Exception):
190+
"""
191+
Error raised for unsupported Numba engine routines.
192+
"""

pandas/tests/groupby/aggregate/test_numba.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import NumbaUtilError
45
import pandas.util._test_decorators as td
56

67
from pandas import DataFrame
@@ -17,10 +18,10 @@ def incorrect_function(x):
1718
{"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
1819
columns=["key", "data"],
1920
)
20-
with pytest.raises(ValueError, match=f"The first 2"):
21+
with pytest.raises(NumbaUtilError, match=f"The first 2"):
2122
data.groupby("key").agg(incorrect_function, engine="numba")
2223

23-
with pytest.raises(ValueError, match=f"The first 2"):
24+
with pytest.raises(NumbaUtilError, match=f"The first 2"):
2425
data.groupby("key")["data"].agg(incorrect_function, engine="numba")
2526

2627

@@ -33,10 +34,10 @@ def incorrect_function(x, **kwargs):
3334
{"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
3435
columns=["key", "data"],
3536
)
36-
with pytest.raises(ValueError, match="numba does not support"):
37+
with pytest.raises(NumbaUtilError, match="numba does not support"):
3738
data.groupby("key").agg(incorrect_function, engine="numba", a=1)
3839

39-
with pytest.raises(ValueError, match="numba does not support"):
40+
with pytest.raises(NumbaUtilError, match="numba does not support"):
4041
data.groupby("key")["data"].agg(incorrect_function, engine="numba", a=1)
4142

4243

pandas/tests/groupby/transform/test_numba.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from pandas.errors import NumbaUtilError
34
import pandas.util._test_decorators as td
45

56
from pandas import DataFrame
@@ -16,10 +17,10 @@ def incorrect_function(x):
1617
{"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
1718
columns=["key", "data"],
1819
)
19-
with pytest.raises(ValueError, match=f"The first 2"):
20+
with pytest.raises(NumbaUtilError, match=f"The first 2"):
2021
data.groupby("key").transform(incorrect_function, engine="numba")
2122

22-
with pytest.raises(ValueError, match=f"The first 2"):
23+
with pytest.raises(NumbaUtilError, match=f"The first 2"):
2324
data.groupby("key")["data"].transform(incorrect_function, engine="numba")
2425

2526

@@ -32,10 +33,10 @@ def incorrect_function(x, **kwargs):
3233
{"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
3334
columns=["key", "data"],
3435
)
35-
with pytest.raises(ValueError, match="numba does not support"):
36+
with pytest.raises(NumbaUtilError, match="numba does not support"):
3637
data.groupby("key").transform(incorrect_function, engine="numba", a=1)
3738

38-
with pytest.raises(ValueError, match="numba does not support"):
39+
with pytest.raises(NumbaUtilError, match="numba does not support"):
3940
data.groupby("key")["data"].transform(incorrect_function, engine="numba", a=1)
4041

4142

pandas/tests/test_errors.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"ParserWarning",
1919
"MergeError",
2020
"OptionError",
21+
"NumbaUtilError",
2122
],
2223
)
2324
def test_exception_importable(exc):

pandas/tests/window/test_apply.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import NumbaUtilError
45
import pandas.util._test_decorators as td
56

67
from pandas import DataFrame, Series, Timestamp, date_range
@@ -134,7 +135,7 @@ def test_invalid_raw_numba():
134135

135136
@td.skip_if_no("numba")
136137
def test_invalid_kwargs_nopython():
137-
with pytest.raises(ValueError, match="numba does not support kwargs with"):
138+
with pytest.raises(NumbaUtilError, match="numba does not support kwargs with"):
138139
Series(range(1)).rolling(1).apply(
139140
lambda x: x, kwargs={"a": 1}, engine="numba", raw=True
140141
)

0 commit comments

Comments
 (0)