From c307128c30f13dfdfba0497174e20890731fb213 Mon Sep 17 00:00:00 2001 From: serhiisavingit <103208330+serhiisavingit@users.noreply.github.com> Date: Fri, 24 Jan 2025 16:53:05 -0500 Subject: [PATCH 1/4] Update boxplot.py --- pandas/plotting/_matplotlib/boxplot.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 80f0349b205e6..34cbea34babc8 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -4,6 +4,8 @@ TYPE_CHECKING, Literal, NamedTuple, + Optional, + Union, ) import warnings @@ -98,7 +100,8 @@ def __init__(self, data, return_type: str = "axes", **kwargs) -> None: # error: Signature of "_plot" incompatible with supertype "MPLPlot" @classmethod def _plot( # type: ignore[override] - cls, ax: Axes, y: np.ndarray, column_num=None, return_type: str = "axes", **kwds + cls, ax: Axes, y: np.ndarray, column_num: Optional[int] = None, return_type: str = "axes", **kwds + ): ys: np.ndarray | list[np.ndarray] if y.ndim == 2: @@ -107,6 +110,9 @@ def _plot( # type: ignore[override] # if any cols are empty # GH 8181 ys = [v if v.size > 0 else np.array([np.nan]) for v in ys] + for v in ys: + if v.size == 0: + warnings.warn("Empty array detected, replacing with NaN", stacklevel=find_stack_level()) else: ys = remove_na_arraylike(y) bp = ax.boxplot(ys, **kwds) @@ -166,10 +172,7 @@ def _caps_c(self): def _get_colors( self, num_colors=None, - color_kwds: dict[str, MatplotlibColor] - | MatplotlibColor - | Collection[MatplotlibColor] - | None = "color", + color_kwds: Optional[Union[dict[str, MatplotlibColor], MatplotlibColor, Collection[MatplotlibColor]]] = "color", ) -> None: pass @@ -391,8 +394,7 @@ def _get_colors(): result[key_to_index[key]] = value else: raise ValueError( - f"color dict contains invalid key '{key}'. " - f"The key must be either {valid_keys}" + f"Invalid key '{key}' in color dictionary. Expected one of {valid_keys}. Please refer to documentation.") ) else: result.fill(colors) From 30eeec94b738f0697f76d5edd8a94adc80516b65 Mon Sep 17 00:00:00 2001 From: serhiisavingit <103208330+serhiisavingit@users.noreply.github.com> Date: Fri, 24 Jan 2025 18:32:01 -0500 Subject: [PATCH 2/4] Updates from editor --- pandas/plotting/_matplotlib/boxplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 34cbea34babc8..bc5ea036008c4 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -394,7 +394,7 @@ def _get_colors(): result[key_to_index[key]] = value else: raise ValueError( - f"Invalid key '{key}' in color dictionary. Expected one of {valid_keys}. Please refer to documentation.") + f"Invalid key '{key}' in color dictionary. Expected one of {valid_keys}. Please refer to documentation." ) else: result.fill(colors) From 9c44e23585010c355a08c18baa671b650b90240a Mon Sep 17 00:00:00 2001 From: serhiisavingit <103208330+serhiisavingit@users.noreply.github.com> Date: Fri, 24 Jan 2025 19:10:28 -0500 Subject: [PATCH 3/4] Refine error message for invalid color key --- pandas/plotting/_matplotlib/boxplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index bc5ea036008c4..184c447af37a3 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -394,7 +394,7 @@ def _get_colors(): result[key_to_index[key]] = value else: raise ValueError( - f"Invalid key '{key}' in color dictionary. Expected one of {valid_keys}. Please refer to documentation." + f"color dict contains invalid key '{key}'. The key must be either {valid_keys}" ) else: result.fill(colors) From 2c95f2cef79ebf906fd035cbad3947fd8f55a942 Mon Sep 17 00:00:00 2001 From: serhiisavingit <103208330+serhiisavingit@users.noreply.github.com> Date: Sun, 26 Jan 2025 11:49:08 -0500 Subject: [PATCH 4/4] Fix line break in `color_kwds` parameter. --- pandas/plotting/_matplotlib/boxplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 184c447af37a3..5ca9aa6d1ab86 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -172,7 +172,8 @@ def _caps_c(self): def _get_colors( self, num_colors=None, - color_kwds: Optional[Union[dict[str, MatplotlibColor], MatplotlibColor, Collection[MatplotlibColor]]] = "color", + color_kwds: Optional[Union[dict[str, MatplotlibColor], + MatplotlibColor, Collection[MatplotlibColor]]] = "color", ) -> None: pass