Skip to content

Commit a4d8b4a

Browse files
Move mock execution class to conftest
1 parent cae63ac commit a4d8b4a

File tree

3 files changed

+57
-59
lines changed

3 files changed

+57
-59
lines changed

pandas/tests/apply/common.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,7 @@
1-
import numpy as np
2-
3-
from pandas import (
4-
DataFrame,
5-
Series,
6-
)
7-
from pandas.api.executors import BaseExecutionEngine
81
from pandas.core.groupby.base import transformation_kernels
92

103
# There is no Series.cumcount or DataFrame.cumcount
114
series_transform_kernels = [
125
x for x in sorted(transformation_kernels) if x != "cumcount"
136
]
147
frame_transform_kernels = [x for x in sorted(transformation_kernels) if x != "cumcount"]
15-
16-
17-
class MockExecutionEngine(BaseExecutionEngine):
18-
"""
19-
Execution Engine to test if the execution engine interface receives and
20-
uses all parameters provided by the user.
21-
22-
Making this engine work as the default Python engine by calling it, no extra
23-
functionality is implemented here.
24-
25-
When testing, this will be called when this engine is provided, and then the
26-
same pandas.map and pandas.apply function will be called, but without engine,
27-
executing the default behavior from the python engine.
28-
"""
29-
30-
def map(data, func, args, kwargs, decorator, skip_na):
31-
kwargs_to_pass = kwargs if isinstance(data, DataFrame) else {}
32-
return data.map(func, na_action="ignore" if skip_na else None, **kwargs_to_pass)
33-
34-
def apply(data, func, args, kwargs, decorator, axis):
35-
if isinstance(data, Series):
36-
return data.apply(func, convert_dtype=True, args=args, by_row=False)
37-
elif isinstance(data, DataFrame):
38-
return data.apply(
39-
func,
40-
axis=axis,
41-
raw=False,
42-
result_type=None,
43-
args=args,
44-
by_row="compat",
45-
**kwargs,
46-
)
47-
else:
48-
assert isinstance(data, np.ndarray)
49-
50-
def wrap_function(func):
51-
# https://github.com/numpy/numpy/issues/8352
52-
def wrapper(*args, **kwargs):
53-
result = func(*args, **kwargs)
54-
if isinstance(result, str):
55-
result = np.array(result, dtype=object)
56-
return result
57-
58-
return wrapper
59-
60-
return np.apply_along_axis(wrap_function(func), axis, data, *args, **kwargs)
61-
62-
63-
class MockEngineDecorator:
64-
__pandas_udf__ = MockExecutionEngine

pandas/tests/apply/conftest.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
1+
import numpy as np
12
import pytest
23

3-
from pandas.tests.apply.common import MockEngineDecorator
4+
from pandas import (
5+
DataFrame,
6+
Series,
7+
)
8+
from pandas.api.executors import BaseExecutionEngine
9+
10+
11+
class MockExecutionEngine(BaseExecutionEngine):
12+
"""
13+
Execution Engine to test if the execution engine interface receives and
14+
uses all parameters provided by the user.
15+
16+
Making this engine work as the default Python engine by calling it, no extra
17+
functionality is implemented here.
18+
19+
When testing, this will be called when this engine is provided, and then the
20+
same pandas.map and pandas.apply function will be called, but without engine,
21+
executing the default behavior from the python engine.
22+
"""
23+
24+
def map(data, func, args, kwargs, decorator, skip_na):
25+
kwargs_to_pass = kwargs if isinstance(data, DataFrame) else {}
26+
return data.map(func, na_action="ignore" if skip_na else None, **kwargs_to_pass)
27+
28+
def apply(data, func, args, kwargs, decorator, axis):
29+
if isinstance(data, Series):
30+
return data.apply(func, convert_dtype=True, args=args, by_row=False)
31+
elif isinstance(data, DataFrame):
32+
return data.apply(
33+
func,
34+
axis=axis,
35+
raw=False,
36+
result_type=None,
37+
args=args,
38+
by_row="compat",
39+
**kwargs,
40+
)
41+
else:
42+
assert isinstance(data, np.ndarray)
43+
44+
def wrap_function(func):
45+
# https://github.com/numpy/numpy/issues/8352
46+
def wrapper(*args, **kwargs):
47+
result = func(*args, **kwargs)
48+
if isinstance(result, str):
49+
result = np.array(result, dtype=object)
50+
return result
51+
52+
return wrapper
53+
54+
return np.apply_along_axis(wrap_function(func), axis, data, *args, **kwargs)
55+
56+
57+
class MockEngineDecorator:
58+
__pandas_udf__ = MockExecutionEngine
459

560

661
@pytest.fixture(params=[None, MockEngineDecorator])

pandas/tests/apply/test_frame_apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
date_range,
1818
)
1919
import pandas._testing as tm
20-
from pandas.tests.apply.common import MockEngineDecorator
20+
from pandas.tests.apply.conftest import MockEngineDecorator
2121
from pandas.tests.frame.common import zip_frames
2222
from pandas.util.version import Version
2323

0 commit comments

Comments
 (0)