Python – Uploading Geodataframe With Missing Geometries Using ‘To_PostGIS’

geoalchemygeopandaspostgispythonsqlalchemy

I have a GeoDataFrame with plenty of info, including a 'geometry' column. Some of the rows have no geometry data… I simply don't have it. I decided to leave those rows as 'None' or 'NoneType' in the geometry column. This would be a representation of my GeoDataFrame:

   person  job         geometry
1  bob     Musician    POINT(1234.1234123 1234123.234)
2  john    Accountant  None
3  Sandra  Banker      POINT(234234.2343 23423.234)
...

I have tried with this:

engine = db.create_engine('postgresql://{}:{}@{}:5432/{}'.format(user, password, host, database))

customers.to_postgis(
    con=engine,
    name="customers_db"
)

But of course, Python complaints:

AttributeError: 'NoneType' object has no attribute '_geom'

How can I still upload the GeoDataFrame? I do not want to get rid of those customers without coordinates as they still have valuable info + I could obtain it later in time.

Best Answer

So thanks to the comments I was able to find an answer. The simplest way to create a 'geometry nonetype' is by creating an empty geometry. This is a representation of what I did:

from shapely.geometry import Point

gdf['geometry'][0] = Point()

That creates an empty geometry value.