I have a pandas DataFrame that I am trying to convert to a GeoDataframe. I have some geometries that are np.nan
values and I get an error when attempting to convert them.
import pandas as pd
import geopandas as gpd
import numpy as np
from shapely import wkt
df = pd.DataFrame({
id: [0, 1, 2, 3],
geo: [
'POLYGON ((35.067912 -106.700884, 35.066714 -106.70128, 35.066555 -106.700697, 35.066492 -106.700468, 35.066134 -106.700367, 35.066088 -106.700129, 35.066062 -106.700005, 35.067519 -106.699412, 35.06763 -106.69937, 35.067659 -106.699537, 35.067737 -106.699977, 35.067814 -106.700378, 35.067891 -106.700773, 35.067912 -106.700884))',
np.nan,
'POLYGON ((35.067912 -106.700884, 35.066714 -106.70128, 35.066555 -106.700697, 35.066492 -106.700468, 35.066134 -106.700367, 35.066088 -106.700129, 35.066062 -106.700005, 35.067519 -106.699412, 35.06763 -106.69937, 35.067659 -106.699537, 35.067737 -106.699977, 35.067814 -106.700378, 35.067891 -106.700773, 35.067912 -106.700884))',
np.nan
]
})
usgeo = gpd.GeoDataFrame(df)
usgeo['geometry'] = usgeo['geom'].apply(wkt.loads)
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/shapely/geos.py in read(self, text)
325 """Returns geometry from WKT"""
326 if not isinstance(text, str):
--> 327 raise TypeError("Only str is accepted.")
328 text = text.encode()
329 c_string = c_char_p(text)
TypeError: Only str is accepted.
How do I convert to geoDataFrame with empty / np.nan
as geometries? I'd prefer not to drop the rows as I care about the other information in the dataframe.
CodePudding user response:
You can't parse np.nan
as wkt. You can however handle this in your apply:
usgeo['geometry'] = usgeo['geom'].apply(
lambda x: wkt.loads(x) if isinstance(x, str) else None
)
See the geopandas docs section on missing and empty geometries for a discussion of the differences between missing values (None
or np.nan
) and empty geometries (e.g. shapely.geometry.GeometryCollection()
). These two have different interpretations in geopandas.