You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建空GeoSeries时出现FutureWarning,指定dtype仍无法消除

消除创建空GeoSeries时的FutureWarning问题

问题描述

尝试创建空GeoSeries后续填充几何图形:

import geopandas as gpd
x = gpd.GeoSeries()

执行后触发以下警告:

FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  s = pd.Series(data, index=index, name=name, **kwargs)

按提示指定dtype='geometry',甚至尝试float64、int等其他类型,警告仍未消除。使用的版本信息:

gpd.show_versions()

SYSTEM INFO
-----------
python     : 3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
executable : xxx
machine    : xxx

GEOS, GDAL, PROJ INFO
--------------------- 
GEOS       : None
GEOS lib   : None
GDAL       : 3.0.2
GDAL data dir: None
PROJ       : 6.2.1
PROJ data dir: xxx

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.9.0
pandas     : 1.5.2
fiona      : 1.8.13.post1
numpy      : 1.23.4
shapely    : 1.8.4
rtree      : 0.9.7
pyproj     : 2.6.1.post1
matplotlib : 3.5.1
mapclassify: 2.4.3
geopy      : None
psycopg2   : None
geoalchemy2: None
pyarrow    : None
pygeos     : None

解决方案

方法1:传入空几何列表初始化

直接用空的几何类型列表初始化GeoSeries,明确指定dtype,绕过底层pandas的默认dtype逻辑:

import geopandas as gpd
x = gpd.GeoSeries([], dtype='geometry')

方法2:升级geopandas版本

该问题是geopandas 0.9.0的已知bug,在0.10.0及以上版本已修复。升级后,直接指定dtype即可消除警告:

pip install --upgrade geopandas

升级后使用如下代码即可:

import geopandas as gpd
x = gpd.GeoSeries(dtype='geometry')

方法3:临时屏蔽特定警告(应急用)

若暂时无法升级,可通过警告过滤器屏蔽该特定FutureWarning:

import geopandas as gpd
import warnings

warnings.filterwarnings("ignore", message="The default dtype for empty Series will be 'object' instead of 'float64' in a future version.")
x = gpd.GeoSeries()

内容的提问来源于stack exchange,提问作者saygdaybob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 16:15:38