【问题标题】:TypeError when writing GeoPandas GeoDataFrame编写 GeoPandas GeoDataFrame 时出现 TypeError
【发布时间】:2022-09-25 15:37:14
【问题描述】:

我有一个 GeoDataFrame 如下:

import pandas as pd
import geopandas
df = pd.DataFrame([[1, 0], [0, 1]], columns=[[\'location\', \'location\'], [\'x\', \'y\']])
df[\"geometry\"] = geopandas.points_from_xy(df[\'location\',\'x\'], df[\'location\', \'y\'])
gdf = geopandas.GeoDataFrame(df, geometry=\"geometry\", crs=\"EPSG:4326\")
gdf

#   location                    geometry
#          x  y                         
# 0        1  0  POINT (1.00000 0.00000)
# 1        0  1  POINT (0.00000 1.00000)

问题:使用 GeoDataFrame to_file 方法会导致不太明确的TypeError

gdf.to_file(\"test.shp\")



# ... 
#
# TypeError: Cannot interpret \'<geopandas.array.GeometryDtype object at 0x7f99fdf45070>\' as # a data type

可能是什么原因,如何解决?

注意:此错误已在另一个 question 中描述,但在不相关的上下文中。

    标签: python pandas geopandas


    【解决方案1】:

    看来GeoDataFrame.to_file 需要一列确切地名称geometry:没有多级列,没有geom,没有geometry_,等等。

    因此,可以通过展平列名来解决上述问题:

    gdf_flatcols = gdf.set_axis([x[0] if x[1] == "" else "_".join(x) for x in gdf.columns], axis=1)
    

    to_file 可以正常工作:

    gdf_flatcols.to_file("test.shp)
    

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      相关资源
      最近更新 更多