Plotly柱状图未正确展示Polars数据框count列数值问题
问题解决:Polars u32类型导致Plotly柱状图数值异常
问题出在count列的u32无符号整数类型上,Plotly对Pandas中的无符号整数类型兼容性不佳,无法正确解析数值渲染图表;而手动指定的列表用的是常规有符号整数,因此显示正常。
解决方法很简单,只需将count列转换为有符号整数类型后再绘制图表:
方法1:在Polars中转换类型后转Pandas
# 将count列转为Int32类型 totals_fixed = totals.with_columns(pl.col('count').cast(pl.Int32)) # 绘制柱状图 px.bar( totals_fixed.to_pandas(), x='commit_year', y='count', color='commit_year' )
方法2:转Pandas后再转换类型
df_pd = totals.to_pandas() # 将count列转为int64类型 df_pd['count'] = df_pd['count'].astype('int64') # 绘制柱状图 px.bar( df_pd, x='commit_year', y='count', color='commit_year' )
转换后count列变为常规有符号整数,Plotly就能正确识别数值并渲染对应的柱状图了。
内容的提问来源于stack exchange,提问作者Adam
相关产品推荐
相关产品推荐

