| 1 | From f4e59d5a12d10b87d55f91e195ec4b8064fa66ca Mon Sep 17 00:00:00 2001
|
| 2 | From: goosfrabba <[email protected]>
|
| 3 | Date: Mon, 6 Jul 2026 10:58:21 -0700
|
| 4 | Subject: [PATCH] Fix DataFrame.applymap deprecation on pandas >= 2.1 without
|
| 5 | dropping pandas < 2.1
|
| 6 |
|
| 7 | pandas >= 2.1 deprecates DataFrame.applymap in favour of DataFrame.map, so parallel_applymap
|
| 8 | raises a FutureWarning (#258). ApplyMap.work now uses DataFrame.map when the installed pandas
|
| 9 | provides it and falls back to applymap otherwise, fixing the deprecation without dropping
|
| 10 | pandas < 2.1 or bumping the minimum Python version (the concern raised on #259). The reference
|
| 11 | computation in test_dataframe_applymap uses the same capability check so it stays warning-free.
|
| 12 | ---
|
| 13 | pandarallel/data_types/dataframe.py | 6 ++++++
|
| 14 | tests/test_pandarallel.py | 4 +++-
|
| 15 | 2 files changed, 9 insertions(+), 1 deletion(-)
|
| 16 |
|
| 17 | diff --git a/pandarallel/data_types/dataframe.py b/pandarallel/data_types/dataframe.py
|
| 18 | index 29c7e50..e1bc627 100644
|
| 19 | |
| 20 | |
| 21 | @@ -66,6 +66,12 @@ def work(
|
| 22 | user_defined_function_kwargs: Dict[str, Any],
|
| 23 | extra: Dict[str, Any],
|
| 24 | ) -> pd.DataFrame:
|
| 25 | + # pandas >= 2.1 deprecates DataFrame.applymap in favour of DataFrame.map (added in 2.1);
|
| 26 | + # older pandas only has applymap. Use whichever this pandas provides, so elementwise
|
| 27 | + # apply keeps working and no longer raises the FutureWarning reported in #258, without
|
| 28 | + # dropping support for pandas < 2.1.
|
| 29 | + if hasattr(data, "map"):
|
| 30 | + return data.map(user_defined_function)
|
| 31 | return data.applymap(user_defined_function)
|
| 32 |
|
| 33 | @staticmethod
|
| 34 | diff --git a/tests/test_pandarallel.py b/tests/test_pandarallel.py
|
| 35 | index 0f91c32..567c7d5 100644
|
| 36 | |
| 37 | |
| 38 | @@ -232,7 +232,9 @@ def test_dataframe_applymap(pandarallel_init, func_dataframe_applymap, df_size):
|
| 39 | )
|
| 40 | df.index = [item / 10 for item in df.index]
|
| 41 |
|
| 42 | - res = df.applymap(func_dataframe_applymap)
|
| 43 | + # pandas >= 2.1 renamed DataFrame.applymap to DataFrame.map; use whichever this pandas
|
| 44 | + # provides so the reference computation stays warning-free across versions.
|
| 45 | + res = (df.map if hasattr(df, "map") else df.applymap)(func_dataframe_applymap)
|
| 46 | res_parallel = df.parallel_applymap(func_dataframe_applymap)
|
| 47 | assert res.equals(res_parallel)
|
| 48 |
|