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