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