[Bug]: `boxplot_stats` raises IndexError when all values are masked · Issue #32508 · matplotlib/matplotlib · GitHub
Skip to content

[Bug]: boxplot_stats raises IndexError when all values are masked #32508

Open

Description

@mansi-tomar-sudo

Bug summary

Bug report

matplotlib.cbook.boxplot_stats() raises an IndexError when given a masked array in which all values are masked.

Minimal reproducible example

import numpy as np
from matplotlib import cbook

data = np.ma.masked_array(
    [10.0, 20.0],
    mask=[True, True]
)

cbook.boxplot_stats(data)

Expected behavior

The function should handle an all-masked dataset gracefully, following the existing empty-data behavior, instead of raising an exception.

Actual behavior

After the masked values are removed, the resulting array is empty. The subsequent percentile calculation raises an IndexError.

Additional context

Partially masked data appears to be covered by the existing test_boxplot_masked test, but an all-masked dataset does not appear to be covered.

I reproduced this behavior using the local Matplotlib checkout. The proposed fix has not yet been submitted for review.

Code for reproduction

import numpy as np
from matplotlib import cbook

data = np.ma.masked_array([10.0, 20.0], mask=[True, True])
cbook.boxplot_stats(data)

Actual outcome

The code raises an IndexError when all elements of the masked array are masked.

Example error with NumPy 2.x:

IndexError: index -1 is out of bounds for axis 0 with size 0

The exact error message may vary depending on the NumPy version.

Expected outcome

The function should handle an array with all values masked gracefully, without raising an exception. It should return appropriate statistics for the empty data remaining after masked values are excluded, consistent with the function's existing empty-data behavior.

Additional information

No response

Operating system

No response

Matplotlib Version

3.11.1

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. lucaznch commented on Oct 20, 2026

    @lucaznch
    Contributor

    Thanks for the report. I'm new to git bisect but assuming I used it correctly it points to f8b3b27

  2. rcomer commented on Oct 20, 2026

    @rcomer
    Member

    Confirmed, the exception came in at f8b3b27, though before that the mask was just ignored so you got spurious statistics. Looks like we just need to move the lines that get the unmasked values above the line that checks for zero-length input.

  3. mansi-tomar-sudo commented on Oct 20, 2026

    @mansi-tomar-sudo
    Author

    Thanks for confirming the issue and identifying the likely cause! I'd like to work on the fix and add a regression test for the all-masked input case. Could you please assign this issue to me? I'd also like to confirm that I'm welcome to submit a PR for this fix.

  4. rcomer commented on Oct 20, 2026

    @rcomer
    Member

    @mansi-tomar-sudo you are welcome to submit a pull request for any issue, but we do not assign them. Please see our contributors’ guide:
    https://matplotlib.org/devdocs/devel/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions