Skip to content

Commit 23d0c14

Browse files
FIX-#7645: Stop raising an error for applying numpy ufuncs. (#7646)
Signed-off-by: sfc-gh-mvashishtha <mahesh.vashishtha@snowflake.com>
1 parent fd2fb44 commit 23d0c14

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

modin/pandas/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ def cast_function_modin2pandas(func):
101101
-------
102102
object
103103
"""
104-
if callable(func):
105-
if func.__module__ == "modin.pandas.series":
104+
if callable(func) and (module := getattr(func, "__module__", None)) is not None:
105+
if module == "modin.pandas.series":
106106
func = getattr(pandas.Series, func.__name__)
107-
elif func.__module__ in ("modin.pandas.dataframe", "modin.pandas.base"):
107+
elif module in ("modin.pandas.dataframe", "modin.pandas.base"):
108108
# FIXME: when the method is defined in `modin.pandas.base` file, then the
109109
# type cannot be determined, in general there may be an error, but at the
110110
# moment it is better.

modin/tests/pandas/dataframe/test_udf.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
_assert_casting_functions_wrap_same_implementation,
2424
)
2525
from modin.tests.pandas.utils import (
26+
UNIVERSAL_UNARY_NUMPY_FUNCTIONS_FOR_FLOATS,
2627
agg_func_except_keys,
2728
agg_func_except_values,
2829
agg_func_keys,
@@ -318,6 +319,22 @@ def test_apply_modin_func_4635():
318319
)
319320

320321

322+
@pytest.mark.parametrize(
323+
"apply_function",
324+
(
325+
lambda df, function: function(df),
326+
lambda df, function: df.apply(function, axis=0),
327+
lambda df, function: df.apply(function, axis=1),
328+
),
329+
)
330+
@pytest.mark.parametrize("function", UNIVERSAL_UNARY_NUMPY_FUNCTIONS_FOR_FLOATS)
331+
def test_apply_unary_numpy_universal_function_issue_7645(function, apply_function):
332+
eval_general(
333+
*create_test_dfs(test_data["float_nan_data"]),
334+
lambda df: apply_function(df, function),
335+
)
336+
337+
321338
def test_eval_df_use_case():
322339
frame_data = {"a": random_state.randn(10), "b": random_state.randn(10)}
323340
df = pandas.DataFrame(frame_data)

modin/tests/pandas/test_series.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from .utils import (
4747
RAND_HIGH,
4848
RAND_LOW,
49+
UNIVERSAL_UNARY_NUMPY_FUNCTIONS_FOR_FLOATS,
4950
CustomIntegerForAddition,
5051
NonCommutativeMultiplyInteger,
5152
agg_func_except_keys,
@@ -5144,14 +5145,19 @@ def test_apply_return_df(data):
51445145

51455146

51465147
@pytest.mark.parametrize(
5147-
"function",
5148-
[
5149-
np.abs,
5150-
np.sin,
5151-
],
5148+
"apply_function",
5149+
(
5150+
lambda series, function: function(series),
5151+
lambda series, function: series.apply(function),
5152+
lambda series, function: series.map(function),
5153+
),
51525154
)
5153-
def test_unary_numpy_universal_function_issue_6483(function):
5154-
eval_general(*create_test_series(test_data["float_nan_data"]), function)
5155+
@pytest.mark.parametrize("function", UNIVERSAL_UNARY_NUMPY_FUNCTIONS_FOR_FLOATS)
5156+
def test_unary_numpy_universal_function_issue_6483_and_7645(function, apply_function):
5157+
eval_general(
5158+
*create_test_series(test_data["float_nan_data"]),
5159+
lambda series: apply_function(series, function),
5160+
)
51555161

51565162

51575163
def test_binary_numpy_universal_function_issue_6483():

modin/tests/pandas/utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,65 @@
306306
join_type_keys = list(join_type.keys())
307307
join_type_values = list(join_type.values())
308308

309+
310+
UNIVERSAL_UNARY_NUMPY_FUNCTIONS_FOR_FLOATS = (
311+
np.negative,
312+
np.abs,
313+
np.sin,
314+
np.positive,
315+
np.absolute,
316+
np.fabs,
317+
np.rint,
318+
np.sign,
319+
np.conj,
320+
np.conjugate,
321+
np.exp,
322+
np.exp2,
323+
np.log,
324+
np.log2,
325+
np.log10,
326+
np.expm1,
327+
np.log1p,
328+
np.sqrt,
329+
np.square,
330+
np.cbrt,
331+
np.reciprocal,
332+
np.sin,
333+
np.cos,
334+
np.tan,
335+
np.arcsin,
336+
np.arccos,
337+
np.arctan,
338+
np.sinh,
339+
np.cosh,
340+
np.tanh,
341+
np.arcsinh,
342+
np.arccosh,
343+
np.arctanh,
344+
np.degrees,
345+
np.radians,
346+
np.deg2rad,
347+
np.rad2deg,
348+
np.logical_not,
349+
np.isfinite,
350+
np.isinf,
351+
np.isnan,
352+
np.fabs,
353+
np.signbit,
354+
np.spacing,
355+
np.floor,
356+
np.ceil,
357+
np.trunc,
358+
)
359+
309360
# Test functions for applymap
310361
test_func = {
311362
"plus one": lambda x: x + 1,
312363
"convert to string": str,
313364
"square": lambda x: x * x,
314365
"identity": lambda x: x,
315366
"return false": lambda x: False,
367+
**{func.__name__: func for func in UNIVERSAL_UNARY_NUMPY_FUNCTIONS_FOR_FLOATS},
316368
}
317369
test_func_keys = list(test_func.keys())
318370
test_func_values = list(test_func.values())

0 commit comments

Comments
 (0)