conftest.py 2.0 KB

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