non87
non87

Reputation: 135

np.nan become None in GeoSeries

It looks like GeoPandas doesn't like np.nan, at least in my version, 0.8.1. When I pass a np.nan into a GeoSeries it becomes a None and I don't know to get the np.nan back.

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

Am I missing something? Is this the expected behavior? Is there a way to get a np.nan within a GeoSeries?

Upvotes: 0

Views: 539

Answers (1)

mosc9575
mosc9575

Reputation: 6347

From the documentation there are two sections which answer your question.

From the section about 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.

From the section about 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.

So the behavior is wanted by geopandas.

Comment

In the latest version it is possible to produce a NaN-value which is not recommended.

If your run gpd.GeoSeries([np.nan, np.nan, pt([1,2]), pt([0,0])]), then the resulting series is of type GeoSeries.

If you mix floats and points likes this gpd.GeoSeries([10, np.nan, pt([1,2]), pt([0,0])]) the result is of type Series which is a fallback right now which contains a NaN value.

In geopandas 0.8.0 this raises a warning: You are passing non-geometry data to the GeoSeries constructor. In future versions this will raise an error.

Upvotes: 2

Related Questions