diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 80f0349b205e6..5ca9aa6d1ab86 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,8 @@ 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 +395,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"color dict contains invalid key '{key}'. The key must be either {valid_keys}" ) else: result.fill(colors)