首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中绘制时间间隔时,无法将IntervalIndex转换为dtype int32错误

在python中绘制时间间隔时,无法将IntervalIndex转换为dtype int32错误
EN

Stack Overflow用户
提问于 2021-01-22 17:12:59
回答 1查看 189关注 0票数 2

我有这样的代码:

代码语言:javascript
复制
plt.subplot(121)
df=pd.DataFrame()
df['age']=[1,2,3,4,5,6,7,8,9,0,2,3,4,5,6,7,8,9,0,4,1,2,3,4,5,6,7,8,9,0]
df['age_cat']=pd.cut(age,bins=list(np.arange(1, 10, 2)))
df.groupby('age_cat')['age'].count().plot(kind='bar')

但是,当我运行这段代码时,我会得到以下错误:

TypeError:无法将IntervalIndex转换为dtype int32

我正在绘制年龄值的直方图。我简化了代码以显示错误。

这个错误似乎与绘图函数有关,就好像我删除了它一样,代码可以工作。

编辑1

将代码更改为:

代码语言:javascript
复制
plt.subplot(121)
df=pd.DataFrame()
df['age']=[1,2,3,4,5,6,7,8,9,0,2,3,4,5,6,7,8,9,0,4,1,2,3,4,5,6,7,8,9,0]
df['age_cat']=pd.cut(df['age'],bins=list(np.arange(1, 10, 2)))
df.groupby('age_cat')['age'].count().plot(kind='bar')   

但仍然是同样的错误信息。

我的库版本是:

代码语言:javascript
复制
absl-py                0.11.0   
astunparse             1.6.3    
async-generator        1.10     
attrs                  20.3.0   
backcall               0.2.0    
bleach                 3.2.1
cachetools             4.2.0
certifi                2020.12.5
chardet                4.0.0
colorama               0.4.4
cycler                 0.10.0
decorator              4.4.2
defusedxml             0.6.0
entrypoints            0.3
flatbuffers            1.12
gast                   0.3.3
google-auth            1.24.0
google-auth-oauthlib   0.4.2
google-pasta           0.2.0
grpcio                 1.32.0
h5py                   2.10.0
idna                   2.10
ipykernel              5.4.3
ipython                7.19.0
ipython-genutils       0.2.0
jedi                   0.18.0
Jinja2                 2.11.2
joblib                 1.0.0
jsonschema             3.2.0
jupyter-client         6.1.11
jupyter-core           4.7.0
jupyterlab-pygments    0.1.2
Keras-Preprocessing    1.1.2
kiwisolver             1.3.1
Markdown               3.3.3
MarkupSafe             1.1.1
matplotlib             3.3.3
mistune                0.8.4
nbclient               0.5.1
nbconvert              6.0.7
nbformat               5.1.2
nest-asyncio           1.4.3
numpy                  1.19.3
oauthlib               3.1.0
opt-einsum             3.3.0
packaging              20.8
pandas                 1.2.0
pandocfilters          1.4.3
parso                  0.8.1
pickleshare            0.7.5
Pillow                 8.1.0
pip                    20.3.3
prompt-toolkit         3.0.10
protobuf               3.14.0
pyasn1                 0.4.8
pyasn1-modules         0.2.8
Pygments               2.7.4
pyparsing              2.4.7
pyrsistent             0.17.3
python-dateutil        2.8.1
pytz                   2020.5
pywin32                300
pyzmq                  21.0.1
requests               2.25.1
requests-oauthlib      1.3.0
rsa                    4.7
scikit-learn           0.24.0
scipy                  1.6.0
seaborn                0.11.1
setuptools             49.2.1
six                    1.15.0
tabulate               0.8.7
tensorboard            2.4.1
tensorboard-plugin-wit 1.7.0
tensorflow             2.4.0
tensorflow-estimator   2.4.0
termcolor              1.1.0
testpath               0.4.4
tf                     1.0.0
threadpoolctl          2.1.0
tornado                6.1
traitlets              5.0.5
typing-extensions      3.7.4.3
urllib3                1.26.2
wcwidth                0.2.5
webencodings           0.5.1
Werkzeug               1.0.1
wheel                  0.36.2
wrapt                  1.12.1
EN

回答 1

Stack Overflow用户

发布于 2021-01-22 17:15:33

看来你切错了东西,应该是df['age'],而不是age

代码语言:javascript
复制
df['age_cat']=pd.cut(df['age'],bins=list(np.arange(1, 10, 2)))

输出:

一些附带说明:

你不需要用np.arange.

  • You包装list
  1. 可以用bins

完成value_counts

df['age'].value_counts(bins=np.arange(1, 10, 2)).plot.bar()

这意味着:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65849724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档