Python – AttributeError: ‘NoneType’ Object Has No Attribute ‘is_empty’ When Setting New CRS

coordinate systemgeopandaspython

I have geodataframe that has two geometry fields.
I'm trying to set new CRS but every time I get the next error message:

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

when I am able to check the original CRS of the layer:

print('original crs is:',tost.crs)

>>>original crs is: EPSG:4326

but then when I try to set new CRS to the copy:

tast = tost.copy()
tast= tast.to_crs({'init': 'epsg:3857'})
print('projected the copy df to:',tast.crs)

>>>AttributeError: 'NoneType' object has no attribute 'is_empty'

I don't understand why is this hapenning.
What cause this error?
My goal here is to be able to set the crs

Best Answer

What if you don't want to drop any rows?
My solution would be to set the null geometries to be empty geometryCollections.
Then the to_crs() method works without throwing any AttributeError.
My code:

from shapely.geometry.collection import GeometryCollection

tast['geometry'] = tast.geometry.apply(lambda x: x if x else GeometryCollection())

tast.to_crs({'init': 'epsg:3857'})