GeoPandas supports, just like in pandas, the concept of missing values (NA or null values). But for geometry values, we have an additional concept of empty geometries:
GeoPandas和pandas一样,支持缺失值(NA或空值)。但是对于几何值,我们有一个额外的空几何值的概念。
- Empty geometries are actual geometry objects but that have no coordinates (and thus also no area, for example). They can for example originate from taking the intersection of two polygons that have no overlap. The scalar object (when accessing a single element of a GeoSeries) is still a Shapely geometry object.空几何是实际的几何对象,但没有坐标(例如,因此也没有面积)。例如,它们可以源自取两个没有重叠的多边形的交集。标量对象(当访问 GeoSeries 的单个元素时)仍然是一个 Shapely 几何对象。
- Missing geometries are unknown values in a GeoSeries. They will typically be propagated in operations (for example in calculations of the area or of the intersection), or ignored in reductions such as
unary_union
. The scalar object (when accessing a single element of a GeoSeries) is the PythonNone
object.缺失的几何图形是 GeoSeries 中的未知值。它们通常会在操作中传播(例如在面积或交集的计算中),或者在诸如 unary_union 的操作中被忽略。标量对象(当访问 GeoSeries 的单个元素时)是 Python None 对象。
Warning:
Starting from GeoPandas v0.6.0, those two concepts are more consistently separated. See below for more details on what changed compared to earlier versions.
从 GeoPandas v0.6.0 开始,这两个概念更加一致地分开。有关与早期版本相比发生的变化的更多详细信息,请参见下文。
Consider the following example GeoSeries with one polygon, one missing value and one empty polygon:
考虑以下具有一个多边形、一个缺失值和一个空多边形的 GeoSeries 示例:
In [1]: from shapely.geometry import Polygon
In [2]: s = geopandas.GeoSeries([Polygon([(0, 0), (1, 1), (0, 1)]), None, Polygon([])])
In [3]: s
Out[3]:
0 POLYGON ((0.000000000 0.000000000, 1.000000000...
1 None
2 GEOMETRYCOLLECTION EMPTY
dtype: geometry
In spatial operations, missing geometries will typically propagate (be missing in the result as well), while empty geometries are treated as a geometry and the result will depend on the operation:
在空间操作中,缺失的几何图形通常会传播(结果中也会缺失),而空几何图形被视为几何图形,结果将取决于操作:
In [4]: s.area
Out[4]:
0 0.5
1 NaN
2 0.0
dtype: float64
In [5]: s.union(Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]))
Out[5]:
0 POLYGON ((1.000000000 1.000000000, 1.000000000...
1 None
2 POLYGON ((0.000000000 0.000000000, 0.000000000...
dtype: geometry
In [6]: s.intersection(Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]))
Out[6]:
0 POLYGON ((0.000000000 0.000000000, 0.000000000...
1 None
2 GEOMETRYCOLLECTION EMPTY
dtype: geometry
The GeoSeries.isna()
method will only check for missing values and not for empty geometries:
GeoSeries.isna() 方法将只检查缺失值而不检查空几何对象:
In [7]: s.isna()
Out[7]:
0 False
1 True
2 False
dtype: bool
On the other hand, if you want to know which values are empty geometries, you can use the GeoSeries.is_empty
attribute:
另一方面,如果你想知道哪些值是空几何,你可以使用 GeoSeries.is_empty 属性:
In [8]: s.is_empty
Out[8]:
0 False
1 False
2 True
dtype: bool
To get only the actual geometry objects that are neither missing nor empty, you can use a combination of both:
要仅获取既不缺失也不为空的有效的几何对象,可以结合使用两者:
In [9]: s.is_empty | s.isna()
Out[9]:
0 False
1 True
2 True
dtype: bool
s[~(s.is_empty | s.isna())]
Out[10]:
0 POLYGON ((0.000000000 0.000000000, 1.000000000...
dtype: geometry