GeoPandas .to_file 给出了错误的列?
GeoPandas .to_file gives wrong column?
我有一个带有一列浮点值的 GeoDataFrame,我想将它们转换为 int 值,然后覆盖 shapefile。
import numpy as np
import pandas as pd
import geopandas as gpd
gdf=gpd.read_file(r'.\folder\gdf.shp')
这个 gdf 有一列浮点值,float_column:
gdf["float_column"]
0 1.307500e+12
1 1.307500e+12
2 1.307500e+12
3 1.307500e+12
4 1.307500e+12
5 1.307500e+12
6 1.307500e+12
7 1.307500e+12
8 1.307500e+12
9 1.307500e+12
然后我应用一个转换:
gdf["int_column"]=[int(x) for x in gdf["float_column"]]
其中有这些值(右变换):
gdf["int_column"]
0 1307500192816
1 1307500170116
2 1307500012418
3 1307500152317
4 1307500141816
5 1307500093417
6 1307500055117
7 1307500081117
8 1307500107717
9 1307500096916
10 1307500213815
然后我保存 gdf:
gdf.to_file(r".\folder\gdf.shp",driver='ESRI Shapefile',crs_wkt=prj)
当我交叉检查结果时 int_column 有这些值:
gdf_try=gpd.read_file(r'.\folder\gdf.shp')
gdf_try["int_column"]
0 2147483647
1 2147483647
2 2147483647
3 2147483647
4 2147483647
5 2147483647
6 2147483647
7 2147483647
8 2147483647
9 2147483647
这看起来太疯狂了!我是不是漏掉了什么很蠢的东西??
如评论中所述,问题是由于 int32
限制。没有推断出正确的数据类型,导致信息丢失。这应该在即将发布的 fiona
版本中解决(geopandas
用于 reading/writing 文件),这将改进 int
类型的处理方式 (https://github.com/Toblerity/Fiona/pull/564) .同时,您可以使用
schema = gpd.io.file.infer_schema(gdf)
schema['properties']['int_column'] = 'int:18'
gdf.to_file('gdf.shp', schema=schema)
我有一个带有一列浮点值的 GeoDataFrame,我想将它们转换为 int 值,然后覆盖 shapefile。
import numpy as np
import pandas as pd
import geopandas as gpd
gdf=gpd.read_file(r'.\folder\gdf.shp')
这个 gdf 有一列浮点值,float_column:
gdf["float_column"]
0 1.307500e+12
1 1.307500e+12
2 1.307500e+12
3 1.307500e+12
4 1.307500e+12
5 1.307500e+12
6 1.307500e+12
7 1.307500e+12
8 1.307500e+12
9 1.307500e+12
然后我应用一个转换:
gdf["int_column"]=[int(x) for x in gdf["float_column"]]
其中有这些值(右变换):
gdf["int_column"]
0 1307500192816
1 1307500170116
2 1307500012418
3 1307500152317
4 1307500141816
5 1307500093417
6 1307500055117
7 1307500081117
8 1307500107717
9 1307500096916
10 1307500213815
然后我保存 gdf:
gdf.to_file(r".\folder\gdf.shp",driver='ESRI Shapefile',crs_wkt=prj)
当我交叉检查结果时 int_column 有这些值:
gdf_try=gpd.read_file(r'.\folder\gdf.shp')
gdf_try["int_column"]
0 2147483647
1 2147483647
2 2147483647
3 2147483647
4 2147483647
5 2147483647
6 2147483647
7 2147483647
8 2147483647
9 2147483647
这看起来太疯狂了!我是不是漏掉了什么很蠢的东西??
如评论中所述,问题是由于 int32
限制。没有推断出正确的数据类型,导致信息丢失。这应该在即将发布的 fiona
版本中解决(geopandas
用于 reading/writing 文件),这将改进 int
类型的处理方式 (https://github.com/Toblerity/Fiona/pull/564) .同时,您可以使用
schema = gpd.io.file.infer_schema(gdf)
schema['properties']['int_column'] = 'int:18'
gdf.to_file('gdf.shp', schema=schema)