Repository: modin-project/modin

Issue:
Speed up test_binary



Discussion:

Resolving patch:
diff --git a/modin/pandas/dataframe.py b/modin/pandas/dataframe.py
--- a/modin/pandas/dataframe.py
+++ b/modin/pandas/dataframe.py
@@ -3086,88 +3086,29 @@ def __delitem__(self, key):
             raise KeyError(key)
         self._update_inplace(new_query_compiler=self._query_compiler.delitem(key))
 
-    def __add__(self, other, axis=None, level=None, fill_value=None):
-        return self.add(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __iadd__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.add(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __radd__(self, other, axis=None, level=None, fill_value=None):
-        return self.radd(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __mul__(self, other, axis=None, level=None, fill_value=None):
-        return self.mul(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __imul__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.mul(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rmul__(self, other, axis=None, level=None, fill_value=None):
-        return self.rmul(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __pow__(self, other, axis=None, level=None, fill_value=None):
-        return self.pow(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __ipow__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.pow(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rpow__(self, other, axis=None, level=None, fill_value=None):
-        return self.rpow(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __sub__(self, other, axis=None, level=None, fill_value=None):
-        return self.sub(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __isub__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.sub(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rsub__(self, other, axis=None, level=None, fill_value=None):
-        return self.rsub(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __floordiv__(self, other, axis=None, level=None, fill_value=None):
-        return self.floordiv(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __ifloordiv__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.floordiv(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rfloordiv__(self, other, axis=None, level=None, fill_value=None):
-        return self.rfloordiv(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __truediv__(self, other, axis=None, level=None, fill_value=None):
-        return self.truediv(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __itruediv__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.truediv(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rtruediv__(self, other, axis=None, level=None, fill_value=None):
-        return self.rtruediv(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __mod__(self, other, axis=None, level=None, fill_value=None):
-        return self.mod(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __imod__(
-        self, other, axis=None, level=None, fill_value=None
-    ):  # pragma: no cover
-        return self.mod(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rmod__(self, other, axis=None, level=None, fill_value=None):
-        return self.rmod(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __div__(self, other, axis=None, level=None, fill_value=None):
-        return self.div(other, axis=axis, level=level, fill_value=fill_value)
-
-    def __rdiv__(self, other, axis=None, level=None, fill_value=None):
-        return self.rdiv(other, axis=axis, level=level, fill_value=fill_value)
+    __add__ = add
+    __iadd__ = add  # pragma: no cover
+    __radd__ = radd
+    __mul__ = mul
+    __imul__ = mul  # pragma: no cover
+    __rmul__ = rmul
+    __pow__ = pow
+    __ipow__ = pow  # pragma: no cover
+    __rpow__ = rpow
+    __sub__ = sub
+    __isub__ = sub  # pragma: no cover
+    __rsub__ = rsub
+    __floordiv__ = floordiv
+    __ifloordiv__ = floordiv  # pragma: no cover
+    __rfloordiv__ = rfloordiv
+    __truediv__ = truediv
+    __itruediv__ = truediv  # pragma: no cover
+    __rtruediv__ = rtruediv
+    __mod__ = mod
+    __imod__ = mod  # pragma: no cover
+    __rmod__ = rmod
+    __div__ = div
+    __rdiv__ = rdiv
 
     @property
     def attrs(self):


Test patch:
diff --git a/modin/pandas/test/dataframe/test_binary.py b/modin/pandas/test/dataframe/test_binary.py
--- a/modin/pandas/test/dataframe/test_binary.py
+++ b/modin/pandas/test/dataframe/test_binary.py
@@ -17,13 +17,12 @@
 import modin.pandas as pd
 
 from modin.pandas.test.utils import (
-    random_state,
-    RAND_LOW,
-    RAND_HIGH,
     df_equals,
     test_data_values,
     test_data_keys,
     eval_general,
+    test_data,
+    create_test_dfs,
 )
 
 pd.DEFAULT_NPARTITIONS = 4
@@ -32,204 +31,108 @@
 matplotlib.use("Agg")
 
 
-def inter_df_math_helper(modin_df, pandas_df, op):
-    # Test dataframe to dataframe
-    try:
-        pandas_result = getattr(pandas_df, op)(pandas_df)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(modin_df)
-    else:
-        modin_result = getattr(modin_df, op)(modin_df)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to int
-    try:
-        pandas_result = getattr(pandas_df, op)(4)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(4)
-    else:
-        modin_result = getattr(modin_df, op)(4)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to float
-    try:
-        pandas_result = getattr(pandas_df, op)(4.0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(4.0)
-    else:
-        modin_result = getattr(modin_df, op)(4.0)
-        df_equals(modin_result, pandas_result)
-
-    # Test transposed dataframes to float
-    try:
-        pandas_result = getattr(pandas_df.T, op)(4.0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df.T, op)(4.0)
-    else:
-        modin_result = getattr(modin_df.T, op)(4.0)
-        df_equals(modin_result, pandas_result)
-
-    frame_data = {
-        "{}_other".format(modin_df.columns[0]): [0, 2],
-        modin_df.columns[0]: [0, 19],
-        modin_df.columns[1]: [1, 1],
-    }
-    modin_df2 = pd.DataFrame(frame_data)
-    pandas_df2 = pandas.DataFrame(frame_data)
-
-    # Test dataframe to different dataframe shape
-    try:
-        pandas_result = getattr(pandas_df, op)(pandas_df2)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(modin_df2)
-    else:
-        modin_result = getattr(modin_df, op)(modin_df2)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe fill value
-    try:
-        pandas_result = getattr(pandas_df, op)(pandas_df2, fill_value=0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(modin_df2, fill_value=0)
-    else:
-        modin_result = getattr(modin_df, op)(modin_df2, fill_value=0)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to list
-    list_test = random_state.randint(RAND_LOW, RAND_HIGH, size=(modin_df.shape[1]))
-    try:
-        pandas_result = getattr(pandas_df, op)(list_test, axis=1)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(list_test, axis=1)
-    else:
-        modin_result = getattr(modin_df, op)(list_test, axis=1)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to series axis=0
-    series_test_modin = modin_df[modin_df.columns[0]]
-    series_test_pandas = pandas_df[pandas_df.columns[0]]
-    try:
-        pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(series_test_modin, axis=0)
-    else:
-        modin_result = getattr(modin_df, op)(series_test_modin, axis=0)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to series axis=1
-    series_test_modin = modin_df.iloc[0]
-    series_test_pandas = pandas_df.iloc[0]
-    try:
-        pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=1)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(series_test_modin, axis=1)
-    else:
-        modin_result = getattr(modin_df, op)(series_test_modin, axis=1)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to list axis=1
-    series_test_modin = series_test_pandas = list(pandas_df.iloc[0])
-    try:
-        pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=1)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(series_test_modin, axis=1)
-    else:
-        modin_result = getattr(modin_df, op)(series_test_modin, axis=1)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to list axis=0
-    series_test_modin = series_test_pandas = list(pandas_df[pandas_df.columns[0]])
-    try:
-        pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(series_test_modin, axis=0)
-    else:
-        modin_result = getattr(modin_df, op)(series_test_modin, axis=0)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to series missing values
-    series_test_modin = modin_df.iloc[0, :-2]
-    series_test_pandas = pandas_df.iloc[0, :-2]
-    try:
-        pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=1)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(series_test_modin, axis=1)
-    else:
-        modin_result = getattr(modin_df, op)(series_test_modin, axis=1)
-        df_equals(modin_result, pandas_result)
-
-    # Test dataframe to series with different index
-    series_test_modin = modin_df[modin_df.columns[0]].reset_index(drop=True)
-    series_test_pandas = pandas_df[pandas_df.columns[0]].reset_index(drop=True)
-    try:
-        pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(series_test_modin, axis=0)
-    else:
-        modin_result = getattr(modin_df, op)(series_test_modin, axis=0)
-        df_equals(modin_result, pandas_result)
-
-    # Level test
-    new_idx = pandas.MultiIndex.from_tuples(
+@pytest.mark.parametrize(
+    "other",
+    [
+        lambda df: 4,
+        lambda df, axis: df.iloc[0] if axis == "columns" else list(df[df.columns[0]]),
+    ],
+    ids=["scalar", "series_or_list"],
+)
+@pytest.mark.parametrize("axis", ["rows", "columns"])
+@pytest.mark.parametrize(
+    "op",
+    [
+        *("add", "radd", "sub", "rsub", "mod", "rmod", "pow", "rpow"),
+        *("truediv", "rtruediv", "mul", "rmul", "floordiv", "rfloordiv"),
+    ],
+)
+def test_math_functions(other, axis, op):
+    data = test_data["float_nan_data"]
+    if (op == "floordiv" or op == "rfloordiv") and axis == "rows":
+        # lambda == "series_or_list"
+        pytest.xfail(reason="different behaviour")
+
+    if op == "rmod" and axis == "rows":
+        # lambda == "series_or_list"
+        pytest.xfail(reason="different behaviour")
+
+    eval_general(
+        *create_test_dfs(data), lambda df: getattr(df, op)(other(df, axis), axis=axis)
+    )
+
+
+@pytest.mark.parametrize(
+    "other",
+    [lambda df: df[: -(2 ** 4)], lambda df: df[df.columns[0]].reset_index(drop=True)],
+    ids=["check_missing_value", "check_different_index"],
+)
+@pytest.mark.parametrize("fill_value", [None, 3.0])
+@pytest.mark.parametrize(
+    "op",
+    [
+        *("add", "radd", "sub", "rsub", "mod", "rmod", "pow", "rpow"),
+        *("truediv", "rtruediv", "mul", "rmul", "floordiv", "rfloordiv"),
+    ],
+)
+def test_math_functions_fill_value(other, fill_value, op):
+    data = test_data["int_data"]
+    modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)
+
+    eval_general(
+        modin_df,
+        pandas_df,
+        lambda df: getattr(df, op)(other(df), axis=0, fill_value=fill_value),
+    )
+
+
+@pytest.mark.parametrize(
+    "op",
+    [
+        *("add", "radd", "sub", "rsub", "mod", "rmod", "pow", "rpow"),
+        *("truediv", "rtruediv", "mul", "rmul", "floordiv", "rfloordiv"),
+    ],
+)
+def test_math_functions_level(op):
+    modin_df = pd.DataFrame(test_data["int_data"])
+    modin_df.index = pandas.MultiIndex.from_tuples(
         [(i // 4, i // 2, i) for i in modin_df.index]
     )
-    modin_df_multi_level = modin_df.copy()
-    modin_df_multi_level.index = new_idx
+
     # Defaults to pandas
     with pytest.warns(UserWarning):
         # Operation against self for sanity check
-        getattr(modin_df_multi_level, op)(modin_df_multi_level, axis=0, level=1)
+        getattr(modin_df, op)(modin_df, axis=0, level=1)
 
 
 @pytest.mark.parametrize(
-    "function",
+    "math_op, alias",
     [
-        "add",
-        "div",
-        "divide",
-        "floordiv",
-        "mod",
-        "mul",
-        "multiply",
-        "pow",
-        "sub",
-        "subtract",
-        "truediv",
-        "__div__",
-        "__add__",
-        "__radd__",
-        "__mul__",
-        "__rmul__",
-        "__pow__",
-        "__rpow__",
-        "__sub__",
-        "__floordiv__",
-        "__rfloordiv__",
-        "__truediv__",
-        "__rtruediv__",
-        "__mod__",
-        "__rmod__",
-        "__rdiv__",
+        ("truediv", "divide"),
+        ("truediv", "div"),
+        ("rtruediv", "rdiv"),
+        ("mul", "multiply"),
+        ("sub", "subtract"),
+        ("add", "__add__"),
+        ("radd", "__radd__"),
+        ("div", "__div__"),
+        ("rdiv", "__rdiv__"),
+        ("truediv", "__truediv__"),
+        ("rtruediv", "__rtruediv__"),
+        ("floordiv", "__floordiv__"),
+        ("rfloordiv", "__rfloordiv__"),
+        ("mod", "__mod__"),
+        ("rmod", "__rmod__"),
+        ("mul", "__mul__"),
+        ("rmul", "__rmul__"),
+        ("pow", "__pow__"),
+        ("rpow", "__rpow__"),
+        ("sub", "__sub__"),
+        ("rsub", "__rsub__"),
     ],
 )
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_math_functions(data, function):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_helper(modin_df, pandas_df, function)
+def test_math_alias(math_op, alias):
+    assert getattr(pd.DataFrame, math_op) == getattr(pd.DataFrame, alias)
 
 
 @pytest.mark.parametrize("other", ["as_left", 4, 4.0, "a"])
@@ -264,104 +167,6 @@ def test_multi_level_comparison(data, op):
         getattr(modin_df_multi_level, op)(modin_df_multi_level, axis=0, level=1)
 
 
-# Test dataframe right operations
-def inter_df_math_right_ops_helper(modin_df, pandas_df, op):
-    try:
-        pandas_result = getattr(pandas_df, op)(4)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(4)
-    else:
-        modin_result = getattr(modin_df, op)(4)
-        df_equals(modin_result, pandas_result)
-
-    try:
-        pandas_result = getattr(pandas_df, op)(4.0)
-    except Exception as e:
-        with pytest.raises(type(e)):
-            getattr(modin_df, op)(4.0)
-    else:
-        modin_result = getattr(modin_df, op)(4.0)
-        df_equals(modin_result, pandas_result)
-
-    new_idx = pandas.MultiIndex.from_tuples(
-        [(i // 4, i // 2, i) for i in modin_df.index]
-    )
-    modin_df_multi_level = modin_df.copy()
-    modin_df_multi_level.index = new_idx
-
-    # Defaults to pandas
-    with pytest.warns(UserWarning):
-        # Operation against self for sanity check
-        getattr(modin_df_multi_level, op)(modin_df_multi_level, axis=0, level=1)
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_radd(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "radd")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rdiv(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rdiv")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rfloordiv(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rfloordiv")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rmod(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rmod")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rmul(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rmul")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rpow(request, data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rpow")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rsub(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rsub")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_rtruediv(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "rtruediv")
-
-
-@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test___rsub__(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-    inter_df_math_right_ops_helper(modin_df, pandas_df, "__rsub__")
-
-
-# END test dataframe right operations
-
-
 def test_equals():
     frame_data = {"col1": [2.9, 3, 3, 3], "col2": [2, 3, 4, 1]}
     modin_df1 = pd.DataFrame(frame_data)