Missing and empty geometries¶

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:

• 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.

• 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 Python None object.

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.

Consider the following example GeoSeries with one polygon, one missing value and one empty polygon:

In : from shapely.geometry import Polygon

In : s = geopandas.GeoSeries([Polygon([(0, 0), (1, 1), (0, 1)]), None, Polygon([])])

In : s
Out:
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 : s.area
Out:
0    0.5
1    NaN
2    0.0
dtype: float64

In : s.union(Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]))
Out:
0    POLYGON ((0.000000000 1.000000000, 1.000000000...
1                                                 None
2    POLYGON ((0.000000000 0.000000000, 0.000000000...
dtype: geometry

In : s.intersection(Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]))
Out:
0    POLYGON ((1.000000000 1.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:

In : s.isna()
Out:
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:

In : s.is_empty
Out:
0    False
1    False
2     True
dtype: bool

To get only the actual geometry objects that are neiter missing nor empty, you can use a combination of both:

In : s.is_empty | s.isna()
Out:
0    False
1     True
2     True
dtype: bool

In : s[~(s.is_empty | s.isna())]
Out:
0    POLYGON ((0.000000000 0.000000000, 1.000000000...
dtype: geometry

Changes since GeoPandas v0.6.0¶

In GeoPandas v0.6.0, the missing data handling was refactored and made more consistent across the library.

Historically, missing (“NA”) values in a GeoSeries could be represented by empty geometric objects, in addition to standard representations such as None and np.nan. At least, this was the case in GeoSeries.isna() or when a GeoSeries got aligned in geospatial operations. But, other methods like dropna() and fillna() did not follow this approach and did not consider empty geometries as missing.

In GeoPandas v0.6.0, the most important change is GeoSeries.isna() no longer treating empty as missing:

• Using the small example from above, the old behaviour treated both the empty as missing geometry as “missing”:

>>> s
0    POLYGON ((0 0, 1 1, 0 1, 0 0))
1                              None
2          GEOMETRYCOLLECTION EMPTY
dtype: object

>>> s.isna()
0    False
1     True
2     True
dtype: bool

• Starting from GeoPandas v0.6.0, it will now only see actual missing values as missing:

In : s.isna()
Out:
0    False
1     True
2    False
dtype: bool

For now, when isna() is called on a GeoSeries with empty geometries, a warning is raised to alert the user of the changed behaviour with an indication how to solve this.

Additionally, the behaviour of GeoSeries.align() changed to use missing values instead of empty geometries to fill non-matching indexes. Consider the following small toy example:

In : from shapely.geometry import Point

In : s1 = geopandas.GeoSeries([Point(0, 0), Point(1, 1)], index=[0, 1])

In : s2 = geopandas.GeoSeries([Point(1, 1), Point(2, 2)], index=[1, 2])

In : s1
Out:
0    POINT (0.000000000 0.000000000)
1    POINT (1.000000000 1.000000000)
dtype: geometry

In : s2
Out:
1    POINT (1.000000000 1.000000000)
2    POINT (2.000000000 2.000000000)
dtype: geometry
• Previously, the align method would use empty geometries to fill values:

>>> s1_aligned, s2_aligned = s1.align(s2)

>>> s1_aligned
0                 POINT (0 0)
1                 POINT (1 1)
2    GEOMETRYCOLLECTION EMPTY
dtype: object

>>> s2_aligned
0    GEOMETRYCOLLECTION EMPTY
1                 POINT (1 1)
2                 POINT (2 2)
dtype: object

This method is used under the hood when performing spatial operations on mis-aligned GeoSeries objects:

>>> s1.intersection(s2)
0    GEOMETRYCOLLECTION EMPTY
1                 POINT (1 1)
2    GEOMETRYCOLLECTION EMPTY
dtype: object

• Starting from GeoPandas v0.6.0, GeoSeries.align() will use missing values to fill in the non-aligned indices, to be consistent with the behaviour in pandas:

In : s1_aligned, s2_aligned = s1.align(s2)

In : s1_aligned
Out:
0    POINT (0.000000000 0.000000000)
1    POINT (1.000000000 1.000000000)
2                               None
dtype: geometry

In : s2_aligned
Out:
0                               None
1    POINT (1.000000000 1.000000000)
2    POINT (2.000000000 2.000000000)
dtype: geometry

This has the consequence that spatial operations will also use missing values instead of empty geometries, which can have a different behaviour depending on the spatial operation:

In : s1.intersection(s2)
Out:
0                               None
1    POINT (1.000000000 1.000000000)
2                               None
dtype: geometry