Repository: dask/dask
File: dask/dataframe/tests/test_ufunc.py (Python)

Code before:
        npfunc(darray, dask).compute().values, npfunc(darray.compute(), pandas.values)
    )


@pytest.mark.parametrize("redfunc", ["sum", "prod", "min", "max", "mean"])
@pytest.mark.parametrize("ufunc", _BASE_UFUNCS)
@pytest.mark.parametrize(
    "pandas",
    [
        pd.Series(np.abs(np.random.randn(100))),
        pd.DataFrame(
            {
                "A": np.random.randint(1, 100, size=20),
                "B": np.random.randint(1, 100, size=20),
                "C": np.abs(np.random.randn(20)),
            }
        ),
    ],
)
def test_ufunc_with_reduction(redfunc, ufunc, pandas):
    dask = dd.from_pandas(pandas, 3)

    np_redfunc = getattr(np, redfunc)
    np_ufunc = getattr(np, ufunc)

    if NUMPY_GE_200 and redfunc == "prod" and ufunc in ("floor", "ceil", "trunc"):
        pytest.skip("Numpy started overflowing while we are casting to float")

    if (
        redfunc == "prod"
        and ufunc in ["conj", "square", "negative", "absolute"]
        and isinstance(pandas, pd.DataFrame)
    ):
        # TODO(pandas) follow pandas behaviour?
        # starting with pandas 1.2.0, the ufunc is applied column-wise, and therefore
        # applied on the integer columns separately, overflowing for those columns
        # (instead of being applied on 2D ndarray that was converted to float)
        pytest.xfail("'prod' overflowing with integer columns in pandas 1.2.0")

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", RuntimeWarning)
        warnings.simplefilter("ignore", FutureWarning)
        if dd._dask_expr_enabled():
            import dask_expr as dx

            assert isinstance(np_redfunc(dask), (dd.DataFrame, dd.Series, dx.Scalar))
        else:
            assert isinstance(
                np_redfunc(dask), (dd.DataFrame, dd.Series, dd.core.Scalar)
            )
        assert_eq(np_redfunc(np_ufunc(dask)), np_redfunc(np_ufunc(pandas)))

Review comment (bug):
Should also add an `and isinstance(pandas, pd.DataFrame)` condition here (similar to the `pytest.xfail` `if`-block immediately below this since it looks like things are still working when using a `pd.Series` 

Code after:
        npfunc(darray, dask).compute().values, npfunc(darray.compute(), pandas.values)
    )


@pytest.mark.parametrize("redfunc", ["sum", "prod", "min", "max", "mean"])
@pytest.mark.parametrize("ufunc", _BASE_UFUNCS)
@pytest.mark.parametrize(
    "pandas",
    [
        pd.Series(np.abs(np.random.randn(100))),
        pd.DataFrame(
            {
                "A": np.random.randint(1, 100, size=20),
                "B": np.random.randint(1, 100, size=20),
                "C": np.abs(np.random.randn(20)),
            }
        ),
    ],
)
def test_ufunc_with_reduction(redfunc, ufunc, pandas):
    dask = dd.from_pandas(pandas, 3)

    np_redfunc = getattr(np, redfunc)
    np_ufunc = getattr(np, ufunc)

    if (
        NUMPY_GE_200
        and redfunc == "prod"
        and ufunc in ("floor", "ceil", "trunc")
        and isinstance(pandas, pd.DataFrame)
    ):
        pytest.skip("Numpy started overflowing while we are casting to float")

    if (
        redfunc == "prod"
        and ufunc in ["conj", "square", "negative", "absolute"]
        and isinstance(pandas, pd.DataFrame)
    ):
        # TODO(pandas) follow pandas behaviour?
        # starting with pandas 1.2.0, the ufunc is applied column-wise, and therefore
        # applied on the integer columns separately, overflowing for those columns
        # (instead of being applied on 2D ndarray that was converted to float)
        pytest.xfail("'prod' overflowing with integer columns in pandas 1.2.0")

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", RuntimeWarning)
        warnings.simplefilter("ignore", FutureWarning)
        if dd._dask_expr_enabled():
            import dask_expr as dx

            assert isinstance(np_redfunc(dask), (dd.DataFrame, dd.Series, dx.Scalar))