【问题标题】:deep copy of GeoDataFrame becomes Pandas DataFrameGeoDataFrame 的深拷贝成为 Pandas DataFrame
【发布时间】:2023-01-19 22:06:44
【问题描述】:

当我deepcopy没有“几何”列的geopandas.GeoDataFrame时,副本变成了pandas.DataFrame。为什么会这样?我查看了 Github 上的主要分支,Pandas 和 Geopandas 都没有覆盖 __deepcopy__

import copy
import geopandas as gpd

empty = gpd.GeoDataFrame()
print("original plain:", type(empty))
print("copied plain:", type(copy.deepcopy(empty)))

geom = gpd.GeoDataFrame(columns=["geometry"])
print("original with geometry:", type(geom))
print("copied with geometry:", type(copy.deepcopy(geom)))

输出:

original plain: <class 'geopandas.geodataframe.GeoDataFrame'>
copied plain: <class 'pandas.core.frame.DataFrame'>
original with geometry: <class 'geopandas.geodataframe.GeoDataFrame'>
copied with geometry: <class 'geopandas.geodataframe.GeoDataFrame'>

【问题讨论】:

    标签: python pandas geopandas deep-copy


    【解决方案1】:

    更喜欢copy(Geo)DataFrame的方法:

    empty = gpd.GeoDataFrame()
    print("original plain:", type(empty))
    print("copied plain:", type(empty.copy(deep=True)))
    
    geom = gpd.GeoDataFrame(columns=["geometry"])
    print("original with geometry:", type(geom))
    print("copied with geometry:", type(geom.copy(deep=True)))
    

    输出:

    original plain: <class 'geopandas.geodataframe.GeoDataFrame'>
    copied plain: <class 'geopandas.geodataframe.GeoDataFrame'>
    original with geometry: <class 'geopandas.geodataframe.GeoDataFrame'>
    copied with geometry: <class 'geopandas.geodataframe.GeoDataFrame'>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2015-01-13
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多