Skip to content

FIX: let pandas IndexInt64 work for boxplot #13398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3926,6 +3926,10 @@ def dopatch(xs, ys, **kwargs):
elif len(positions) != N:
raise ValueError(datashape_message.format("positions"))

positions = np.array(positions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good fix for the case described in the ticket, but I think the code will continue raising obscure error when positions are not numbers. Can you add a check after this line about the dtype of the array being a number? Maybe something like:

if not isinstance(positions.dtype, numbers.Number):
     raise TypeError("positions should be a iterable of dtype number, but found dtype %s" % blah blah blah.)

When we end up supporting something else than numbers, this can be updated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't quite get that check to work; I had to check the first element. But if there is a better way, let me know.

if len(positions) > 0 and not isinstance(positions[0], Number):
raise TypeError("positions should be an iterable of numbers")

# width
if widths is None:
widths = [np.clip(0.15 * np.ptp(positions), 0.15, 0.5)] * N
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,15 @@ def test_bar_timedelta():
(10, 20))


def test_boxplot_dates_pandas(pd):
# smoke test for boxplot and dates in pandas
data = np.random.rand(5, 2)
years = pd.date_range('1/1/2000',
periods=2, freq=pd.DateOffset(years=1)).year
plt.figure()
plt.boxplot(data, positions=years)


def test_bar_pandas(pd):
# Smoke test for pandas

Expand Down