3

I am using GeoPandas to create a new database, and I can't assign a multiLineString to the first row of the geometry field. If I assign any other geometry object like a point, lineString, etc. the program works fine. The error says:

TypeError: float() argument must be a string or a number, not 'LineString'

The coordinates of the multiLine I tested is the following one:

multiline_coords = [[(508486.838170367, 4790002.50137761), (508487.239570694, 4790001.69385438), (508487.690689054, 4790000.91301623), (508488.189759189, 4790000.16192038), (508488.734827094, 4789999.44350756), (508489.323758671, 4789998.7605906), (508489.954248081, 4789998.11584329), (508490.623826775, 4789997.51179001), (508491.329873159, 4789996.95079581), (508492.069622856, 4789996.43505714), (508492.840179532, 4789995.96659327), (508493.725328316, 4789995.5931483), (508493.74652957113, 4789995.589901128), (508493.74652957113, 4789995.589901128)], [(508493.74652957113, 4789995.589901128), (508493.746529573, 4789995.589901128), (508494.674957384, 4789995.44770363), (508495.631302006, 4789995.53910649), (508496.536188953, 4789995.86179694), (508497.3345751, 4789996.39614615), (508497.977895633, 4789997.10965033), (508498.42701818, 4789997.95890792), (508498.654623186, 4789998.89225966), (508498.646865729, 4789999.85293096), (508498.404217684, 4790000.78248539), (508497.71657701326, 4790001.97333336), (508497.71657701326, 4790001.97333336)]]

And the creation and assign of the line is:

import geopandas as gpd
import shapely.geometry as spg
import fiona as fi
newdata = gpd.GeoDataFrame()
newdata['geometry'] = None
newdata.crs = fi.crs.from_epsg(4326)
multiline_coords = [[(508486.838170367, 4790002.50137761), (508487.239570694, 4790001.69385438), (508487.690689054, 4790000.91301623), (508488.189759189, 4790000.16192038), (508488.734827094, 4789999.44350756), (508489.323758671, 4789998.7605906), (508489.954248081, 4789998.11584329), (508490.623826775, 4789997.51179001), (508491.329873159, 4789996.95079581), (508492.069622856, 4789996.43505714), (508492.840179532, 4789995.96659327), (508493.725328316, 4789995.5931483), (508493.74652957113, 4789995.589901128), (508493.74652957113, 4789995.589901128)], [(508493.74652957113, 4789995.589901128), (508493.746529573, 4789995.589901128), (508494.674957384, 4789995.44770363), (508495.631302006, 4789995.53910649), (508496.536188953, 4789995.86179694), (508497.3345751, 4789996.39614615), (508497.977895633, 4789997.10965033), (508498.42701818, 4789997.95890792), (508498.654623186, 4789998.89225966), (508498.646865729, 4789999.85293096), (508498.404217684, 4790000.78248539), (508497.71657701326, 4790001.97333336), (508497.71657701326, 4790001.97333336)]]
multi_line = spg.MultiLineString(multiline_coords)
newdata.loc[0, 'geometry'] = multi_line

Complete error:

File "demo.py", line ..., in <module> newdata.loc[0, 'geometry'] = multi_line File "/home/ubuntu/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 189, in __setitem__ self._setitem_with_indexer(indexer, value) File "/home/ubuntu/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 651, in _setitem_with_indexer value=value) File "/home/ubuntu/.local/lib/python3.6/site-packages/pandas/core/internals.py", line 3693, in setitem return self.apply('setitem', **kwargs) File "/home/ubuntu/.local/lib/python3.6/site-packages/pandas/core/internals.py", line 3581, in apply applied = getattr(b, f)(**kwargs) File "/home/ubuntu/.local/lib/python3.6/site-packages/pandas/core/internals.py", line 881, in setitem arr_value = np.array(value) TypeError: float() argument must be a string or a number, not 'LineString'

I searched this error, but the main results are about generic python error. The thing is I don't understand this in this specific context.

EXAMPLE OF SOLUTION PROPOSED BY JORIS

As said by Joris, you have to define the geometry field the same time with a list, but the other fields you can declare them like other times, row by row. An example I implemented:

multiline_coords = [[(508486.838170367, 4790002.50137761), (508487.239570694, 4790001.69385438), (508487.690689054, 4790000.91301623), (508488.189759189, 4790000.16192038), (508488.734827094, 4789999.44350756), (508489.323758671, 4789998.7605906), (508489.954248081, 4789998.11584329), (508490.623826775, 4789997.51179001), (508491.329873159, 4789996.95079581), (508492.069622856, 4789996.43505714), (508492.840179532, 4789995.96659327), (508493.725328316, 4789995.5931483), (508493.74652957113, 4789995.589901128), (508493.74652957113, 4789995.589901128)], [(508493.74652957113, 4789995.589901128), (508493.746529573, 4789995.589901128), (508494.674957384, 4789995.44770363), (508495.631302006, 4789995.53910649), (508496.536188953, 4789995.86179694), (508497.3345751, 4789996.39614615), (508497.977895633, 4789997.10965033), (508498.42701818, 4789997.95890792), (508498.654623186, 4789998.89225966), (508498.646865729, 4789999.85293096), (508498.404217684, 4790000.78248539), (508497.71657701326, 4790001.97333336), (508497.71657701326, 4790001.97333336)]]
multi_line = spg.MultiLineString(multiline_coords)
geoms = [multi_line,multi_line]
newdata = gpd.GeoDataFrame(columns=['name','geometry'])
for i in range(0,2):
    newdata.loc[i,'name'] = str(i)
newdata['geometry'] = geoms
1

Assigning values that itself are seen as a list-like or collection (in this case the MultiLineString) is a bit tricky in pandas, and gives rise to corner cases / bugs as you run into here.

But, in this case, it is better to not first create an empty dataframe, and then assign values to it. You can first create the values, and then construct a dataframe of it:

>>> geoms = [multi_line]

>>> newdata = gpd.GeoDataFrame(geometry=geoms)

>>> newdata
                                            geometry
0  (LINESTRING (508486.838170367 4790002.50137761...
2
  • Oh, thx a ton. For anyone who wants more than one line in the data make a list geoms = [multi_line1, multi_line2, ...] Jun 20 '18 at 7:54
  • so annoying..just spent like an hour trying to figure out why the heck I couldnt assign the linestring to an empty geometry column and did a work around that was basically this. thanks for your answer
    – Derek Eden
    Nov 15 '19 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.