Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Assignees
tacaswell
Milestone
@8one6

Description

@8one6
importpandasaspdimportmatplotlib.pyplotaspltser_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.pycinhist(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'->5604ifisinstance(x,np.ndarray)ornotiterable(x[0]):5605# TODO: support masked arrays;5606x=np.asarray(x)

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

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp