Polars to_dicts()调用失败,其他方法可正常运行求助
用Polars第一行重命名列时
to_dicts()报错的解决建议 尝试用Polars DataFrame的第一行重命名列,调用my_df.head(1).to_dicts()时触发如下报错:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\dataframe\frame.py", line 1973, in to_dicts return list(self.iter_rows(named=True)) File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\dataframe\frame.py", line 8383, in iter_rows yield from zerocopy_slice.to_arrow().to_pylist() AttributeError: 'pyarrow.lib.Table' object has no attribute 'to_pylist' >>> import pyarrow >>> df_addr_parsed2.head(1).to_dicts() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\dataframe\frame.py", line 1973, in to_dicts return list(self.iter_rows(named=True)) File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\dataframe\frame.py", line 8383, in iter_rows yield from zerocopy_slice.to_arrow().to_pylist() AttributeError: 'pyarrow.lib.Table' object has no attribute 'to_pylist'
环境信息:Windows系统、Python 3.9、Polars 0.18、pyarrow 4.0.1。已尝试导入pyarrow库,使用os.add_dll_directory()处理DLL问题,但无效。
解决建议
升级pyarrow版本:pyarrow 4.0.1中
pyarrow.Table没有to_pylist()方法,该方法在pyarrow 5.0.0及以上版本才被支持。执行以下命令升级:pip install --upgrade pyarrow>=5.0.0升级后
to_dicts()即可正常调用。替代实现(不升级依赖):绕开
to_dicts(),直接通过行数据生成列名映射:# 获取第一行作为新列名 new_col_names = df.head(1).row(0) # 重命名列并删除原第一行 df = df.rename(dict(zip(df.columns, new_col_names))).drop(index=0)检查版本兼容性:Polars 0.18的官方依赖要求中,pyarrow最低版本可能高于4.0.1,建议确认对应版本的兼容列表,确保依赖版本匹配。
内容的提问来源于stack exchange,提问作者user13752384
相关产品推荐
相关产品推荐

