| 2860 | |
| 2861 | |
| 2862 | def test_apply(): |
| 2863 | df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [10, 20, 30, 40]}) |
| 2864 | ddf = dd.from_pandas(df, npartitions=2) |
| 2865 | |
| 2866 | assert_eq( |
| 2867 | ddf.x.apply(lambda x: x + 1, meta=("x", int)), df.x.apply(lambda x: x + 1) |
| 2868 | ) |
| 2869 | |
| 2870 | # specify meta |
| 2871 | assert_eq( |
| 2872 | ddf.apply(lambda xy: xy.iloc[0] + xy.iloc[1], axis=1, meta=(None, int)), |
| 2873 | df.apply(lambda xy: xy.iloc[0] + xy.iloc[1], axis=1), |
| 2874 | ) |
| 2875 | assert_eq( |
| 2876 | ddf.apply(lambda xy: xy.iloc[0] + xy.iloc[1], axis="columns", meta=(None, int)), |
| 2877 | df.apply(lambda xy: xy.iloc[0] + xy.iloc[1], axis="columns"), |
| 2878 | ) |
| 2879 | |
| 2880 | # inference |
| 2881 | with warnings.catch_warnings(): |
| 2882 | warnings.simplefilter("ignore", UserWarning) |
| 2883 | assert_eq( |
| 2884 | ddf.apply(lambda xy: xy.iloc[0] + xy.iloc[1], axis=1), |
| 2885 | df.apply(lambda xy: xy.iloc[0] + xy.iloc[1], axis=1), |
| 2886 | ) |
| 2887 | with warnings.catch_warnings(): |
| 2888 | warnings.simplefilter("ignore", UserWarning) |
| 2889 | assert_eq(ddf.apply(lambda xy: xy, axis=1), df.apply(lambda xy: xy, axis=1)) |
| 2890 | |
| 2891 | # specify meta |
| 2892 | func = lambda x: pd.Series([x, x]) |
| 2893 | assert_eq(ddf.x.apply(func, meta=[(0, int), (1, int)]), df.x.apply(func)) |
| 2894 | # inference |
| 2895 | with warnings.catch_warnings(): |
| 2896 | warnings.simplefilter("ignore", UserWarning) |
| 2897 | assert_eq(ddf.x.apply(func), df.x.apply(func)) |
| 2898 | |
| 2899 | # axis=0 |
| 2900 | with pytest.raises(NotImplementedError): |
| 2901 | ddf.apply(lambda xy: xy, axis=0) |
| 2902 | |
| 2903 | with pytest.raises(NotImplementedError): |
| 2904 | ddf.apply(lambda xy: xy, axis="index") |
| 2905 | |
| 2906 | |
| 2907 | def test_apply_warns(): |