Skip to content
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

plt.hist throws KeyError when passed a pandas.Series without 0 in index #5557

Closed
8one6 opened this issue Nov 24, 2015 · 1 comment
Closed
Assignees
Milestone

Comments

@8one6
Copy link

8one6 commented Nov 24, 2015

import pandas as pd
import matplotlib.pyplot as plt

ser_1 = pd.Series(data=[1, 2, 2, 3, 3, 4, 4, 4, 4, 5])
ser_2 = ser_1.iloc[1:]
fig, axes = plt.subplots(1, 4, figsize=(16, 4))
axes[0].hist(ser_1.values)
axes[1].hist(ser_1)
axes[2].hist(ser_2.values)
axes[3].hist(ser_2)

This snippet correctly plots in the first three cases, but errors in the last case, saying (in the relevant part):

/venv/local/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc
in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, 
align, orientation, rwidth, log, color, label, stacked, **kwargs)
   5602         # Massage 'x' for processing.
   5603         # NOTE: Be sure any changes here is also done below to 'weights'
-> 5604         if isinstance(x, np.ndarray) or not iterable(x[0]):
   5605             # TODO: support masked arrays;
   5606             x = np.asarray(x)

(which then descends into a KeyError in the bowels of pandas).

So seems like the issue here is that x[0] is being used to find the first element of the input data (which would work fine if the input data is a numpy.ndarray). But in this case x is a pandas.Series and as a result x[0] is an index lookup rather than a normal slice. This works fine on axes[1] since in that case the histogrammed series includes 0 in its index. However, on axes[3], the histogrammed data no longer includes 0 in the index and things fall apart.

@8one6
Copy link
Author

8one6 commented Dec 7, 2015

xref #5550...

As suggested by @tacaswell in that thread, perhaps the more robust way to get the first element of the passed data is to request next(iter(x)) rather than simply requesting x[0].

@tacaswell tacaswell added this to the Critical bugfix release (1.5.1) milestone Dec 21, 2015
@tacaswell tacaswell self-assigned this Dec 22, 2015
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Dec 22, 2015
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Dec 28, 2015
ilia-kats added a commit to ilia-kats/CombinatorialProfiler that referenced this issue Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants