From f0ebdfbd6387a2b1cf23423f60dd17041c6c3949 Mon Sep 17 00:00:00 2001 From: Hagen Wierstorf Date: Fri, 26 Apr 2024 08:27:30 +0200 Subject: [PATCH 1/2] Suppress pandas FutureWarning for applymap() --- audplot/core/api.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/audplot/core/api.py b/audplot/core/api.py index eadacb9..96d6808 100644 --- a/audplot/core/api.py +++ b/audplot/core/api.py @@ -1,5 +1,6 @@ import math import typing +import warnings import matplotlib import matplotlib.pyplot as plt @@ -174,10 +175,15 @@ def confusion_matrix( cm = pd.DataFrame(cm, index=labels) # Set format of first row labels in confusion matrix - if percentage: - annot = cm.applymap(lambda x: f"{100 * x:.0f}%") - else: - annot = cm.applymap(lambda x: human_format(x)) + with warnings.catch_warnings(): + # Catch warning, + # to still support older pandas versions. + # See https://github.com/audeering/audplot/pull/69 + warnings.simplefilter(action='ignore', category=FutureWarning) + if percentage: + annot = cm.applymap(lambda x: f"{100 * x:.0f}%") + else: + annot = cm.applymap(lambda x: human_format(x)) # Add a second row of annotations if requested if show_both: @@ -188,10 +194,16 @@ def confusion_matrix( normalize=not percentage, ) cm2 = pd.DataFrame(cm2, index=labels) - if percentage: - annot2 = cm2.applymap(lambda x: human_format(x)) - else: - annot2 = cm2.applymap(lambda x: f"{100 * x:.0f}%") + + with warnings.catch_warnings(): + # Catch warning, + # to still support older pandas versions. + # See https://github.com/audeering/audplot/pull/69 + warnings.simplefilter(action='ignore', category=FutureWarning) + if percentage: + annot2 = cm2.applymap(lambda x: human_format(x)) + else: + annot2 = cm2.applymap(lambda x: f"{100 * x:.0f}%") # Combine strings from two dataframes # by vectorizing the underlying function. From f4435a56c9a2a70b7953207aba76463e96c9d003 Mon Sep 17 00:00:00 2001 From: Hagen Wierstorf Date: Fri, 26 Apr 2024 08:30:41 +0200 Subject: [PATCH 2/2] Fix linter --- audplot/core/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/audplot/core/api.py b/audplot/core/api.py index 96d6808..5715598 100644 --- a/audplot/core/api.py +++ b/audplot/core/api.py @@ -179,7 +179,7 @@ def confusion_matrix( # Catch warning, # to still support older pandas versions. # See https://github.com/audeering/audplot/pull/69 - warnings.simplefilter(action='ignore', category=FutureWarning) + warnings.simplefilter(action="ignore", category=FutureWarning) if percentage: annot = cm.applymap(lambda x: f"{100 * x:.0f}%") else: @@ -199,7 +199,7 @@ def confusion_matrix( # Catch warning, # to still support older pandas versions. # See https://github.com/audeering/audplot/pull/69 - warnings.simplefilter(action='ignore', category=FutureWarning) + warnings.simplefilter(action="ignore", category=FutureWarning) if percentage: annot2 = cm2.applymap(lambda x: human_format(x)) else: