np.nan 在 GeoSeries 中变为 None

np.nan become None in GeoSeries

看起来 GeoPandas 不喜欢 np.nan,至少在我的版本中,0.8.1。当我将 np.nan 传递给 GeoSeries 时,它变成了 None 并且我不知道如何取回 np.nan

import geopandas as gpd
from shapely.geometry import Point as pt
import numpy as np

lt = [np.nan, np.nan, pt([1,2]), pt([0,0])]
ser = gpd.GeoSeries(lt)

# Somehow the first two elements of ser became None
ser[0] is None #True
np.isnan(ser[0]) #TypeError

# I don't even know how to put a np.nan in the GeoSeries
ser[0] = np.nan # This doesn't work
# These tests still give the same results
ser[0] is None #True
np.isnan(ser[0]) #TypeError

我错过了什么吗?这是预期的行为吗?有没有办法在 GeoSeries 中获得 np.nan

文档中有两个部分可以回答您的问题。

来自关于 GeoSeries

的部分

A GeoSeries is essentially a vector where each entry in the vector is a set of shapes corresponding to one observation.

geopandas has three basic classes of geometric objects (which are actually shapely objects):

  1. Points / Multi-Points
  2. Lines / Multi-Lines
  3. Polygons / Multi-Polygons

Note that all entries in a GeoSeries need not be of the same geometric type, although certain export operations will fail if this is not the case.

来自关于 missing and empty geometries

的部分

Missing geometries are unknown values in a GeoSeries. The scalar object (when accessing a single element of a GeoSeries) is the Python None object.

所以这种行为是 geopandas 想要的。

评论

在最新版本中,可以生成不推荐的 NaN 值。

如果你的 运行 gpd.GeoSeries([np.nan, np.nan, pt([1,2]), pt([0,0])]),那么结果系列的类型是 GeoSeries.

如果像这样 gpd.GeoSeries([10, np.nan, pt([1,2]), pt([0,0])]) 混合浮点数和点数,结果是 Series 类型,它现在是一个后备,它包含一个 NaN 值。

geopandas 0.8.0 中,这会引发警告:You are passing non-geometry data to the GeoSeries constructor. 在未来的版本中,这会引发错误。