-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Support third-party execution engines in Series.map #61467
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
Merged
datapythonista
merged 12 commits into
pandas-dev:main
from
datapythonista:series_map_engine
May 27, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f61d7b
ENH: Adding engine parameter to Series.map
datapythonista ea45245
Merge remote-tracking branch 'upstream/main' into series_map_engine
datapythonista ef62074
Add missing file
datapythonista b5e5519
Fixing bug when executor returns a numpy array
datapythonista 30ca3bd
engine with no function and tests
datapythonista b32ae65
Merge remote-tracking branch 'upstream/main' into series_map_engine
datapythonista c3afd05
Merge from main
datapythonista 4a3bcfa
Last fixes
datapythonista e838c4c
Fix CI
datapythonista cae63ac
Add fixture import back
datapythonista a4d8b4a
Move mock execution class to conftest
datapythonista 56c3ce0
Adding commit about imported fixture
datapythonista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
) | ||
from pandas.api.executors import BaseExecutionEngine | ||
|
||
|
||
class MockExecutionEngine(BaseExecutionEngine): | ||
""" | ||
Execution Engine to test if the execution engine interface receives and | ||
uses all parameters provided by the user. | ||
|
||
Making this engine work as the default Python engine by calling it, no extra | ||
functionality is implemented here. | ||
|
||
When testing, this will be called when this engine is provided, and then the | ||
same pandas.map and pandas.apply function will be called, but without engine, | ||
executing the default behavior from the python engine. | ||
""" | ||
|
||
def map(data, func, args, kwargs, decorator, skip_na): | ||
kwargs_to_pass = kwargs if isinstance(data, DataFrame) else {} | ||
return data.map(func, na_action="ignore" if skip_na else None, **kwargs_to_pass) | ||
|
||
def apply(data, func, args, kwargs, decorator, axis): | ||
if isinstance(data, Series): | ||
return data.apply(func, convert_dtype=True, args=args, by_row=False) | ||
elif isinstance(data, DataFrame): | ||
return data.apply( | ||
func, | ||
axis=axis, | ||
raw=False, | ||
result_type=None, | ||
args=args, | ||
by_row="compat", | ||
**kwargs, | ||
) | ||
else: | ||
assert isinstance(data, np.ndarray) | ||
|
||
def wrap_function(func): | ||
# https://github.com/numpy/numpy/issues/8352 | ||
def wrapper(*args, **kwargs): | ||
result = func(*args, **kwargs) | ||
if isinstance(result, str): | ||
result = np.array(result, dtype=object) | ||
return result | ||
|
||
return wrapper | ||
|
||
return np.apply_along_axis(wrap_function(func), axis, data, *args, **kwargs) | ||
|
||
|
||
class MockEngineDecorator: | ||
__pandas_udf__ = MockExecutionEngine | ||
|
||
|
||
@pytest.fixture(params=[None, MockEngineDecorator]) | ||
def engine(request): | ||
return request.param |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why this needs importing since it's already in the
conftest.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I answered in a comment, so readers of that input don't need to ask themselves it. Most tests related to apply/map are in
tests/apply
, so the fixture is defined there. But it's also useful here intests/series/methods/
.test/apply/conftest.py
is not in scope when runningtests/series/methods
, so I need to import manually in order to use it. Another alternative would be to move the fixture to the globalconftest.py
, but I think this approach keeps things better organized and simple.I moved the mock classes to
conftest.py
as suggested, thanks for the review.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if your work in #61125 continues to expand, I would be OK moving this to the global
conftest.py