Skip to content

Commit 788da15

Browse files
committed
fix: compat helper for DataFrame map vs applymap across pandas versions
pandas 2.1+ removed applymap; Python 3.8 CI cannot install pandas>=2.1. Use applymap when present, else map.
1 parent 6dec3d5 commit 788da15

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

pyfair/report/base_report.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
from ..utility.beta_pert import FairBetaPert
2323

2424

25+
def _dataframe_elementwise(df, func):
26+
"""Element-wise transform; pandas 2.1+ uses map, older uses applymap."""
27+
applymap = getattr(df, "applymap", None)
28+
if applymap is not None:
29+
return applymap(func)
30+
return df.map(func)
31+
32+
2533
class FairBaseReport(object):
2634
"""A base class for creating FairModel and FairMetaModel reports
2735
@@ -262,8 +270,8 @@ def _get_overview_table(self, model_or_models):
262270
risk_results = risk_results.agg(["mean", "std", "min", "max"])
263271
risk_results.index = ["Mean", "Stdev", "Minimum", "Maximum"]
264272
# Format risk results into dataframe
265-
overview_df = risk_results.map(
266-
lambda x: self._format_strings["Risk"].format(x)
273+
overview_df = _dataframe_elementwise(
274+
risk_results, lambda x: self._format_strings["Risk"].format(x)
267275
)
268276
overview_df.loc["Simulations"] = [
269277
"{0:,.0f}".format(len(model.export_results()))
@@ -321,7 +329,9 @@ def _get_model_parameter_table(self, model):
321329
# On a column basis
322330
axis=1,
323331
)
324-
param_df = param_df.map(lambda x: "" if "nan" in x else x)
332+
param_df = _dataframe_elementwise(
333+
param_df, lambda x: "" if "nan" in x else x
334+
)
325335
# Do not truncate our base64 images.
326336
pd.set_option("display.max_colwidth", None)
327337
# Create our distribution icons as strings in table

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pandas>=2.1.0
1+
pandas>=0.24.1
22
numpy>=1.16.1
33
scipy>=1.2.1
44
matplotlib>=3.0.2

0 commit comments

Comments
 (0)