Pandas空间数据框加载失败:无法将非有限值转换为整数
问题诊断与解决方案
可能原因及对应解决办法:
库版本更新导致逻辑变更
近期ArcGIS API for Python(或依赖的pandas库)的版本更新,可能调整了空间DataFrame的类型推断规则——之前能兼容空值的整型字段,现在会因为空值(以浮点数NaN形式存在)触发类型转换错误。
解决:回退到之前正常运行的版本,执行命令:pip install arcgis==[你2022年8月使用的版本号]显式指定字段数据类型
放弃自动类型推断,手动指定存在空值的整型字段为浮点型,避免转换冲突:
处理在线图层:# 替换成你实际有问题的字段名 dtype_map = {'target_integer_field': float} tagol_df = pd.DataFrame.spatial.from_layer(fLayer, dtype=dtype_map)处理本地要素类:
dtype_map = {'target_integer_field': float} gdb_df = pd.DataFrame.spatial.from_featureclass(lyrPath, dtype=dtype_map)数据源字段定义被修改
检查托管图层或本地GDB的字段类型——可能有人修改了字段定义(比如从浮点型改成整型),但数据里仍遗留空值,导致转换失败。可以通过ArcGIS Pro或ArcCatalog查看字段属性,确认是否和之前一致。预处理空值(谨慎使用)
如果业务允许,先将空值替换为整型可接受的默认值(比如0)再转换,注意这种方法会丢失空值信息:# 以在线图层为例,替换指定字段的空值 null_features = fLayer.query(where='target_integer_field IS NULL').features for feat in null_features: feat.attributes['target_integer_field'] = 0 fLayer.edit_features(updates=null_features) # 再创建DataFrame tagol_df = pd.DataFrame.spatial.from_layer(fLayer)
内容的提问来源于stack exchange,提问作者Sarah Strack
相关产品推荐
相关产品推荐

