Python RecursionError:使用pandas时,相比之下超过了最大递归深度

Python RecursionError:使用pandas时,相比之下超过了最大递归深度,python,pandas,geopandas,shapely,Python,Pandas,Geopandas,Shapely,我正在处理一些地理数据。我想从我已有的纬度和经度中得到一个点对象。我的代码如下所示: for i in range(len(map_polygon)): x = map_polygon.at[i, 'lon'] y = map_polygon.at[i, 'lat'] map_polygon.at[i, 'point'] = Point(x, y) 但是,我得到以下错误: RecursionError:比较中超出了最大递归深度 我做错了什么?数据帧的长度只有大约20k

我正在处理一些地理数据。我想从我已有的纬度和经度中得到一个点对象。我的代码如下所示:

for i in range(len(map_polygon)):
    x = map_polygon.at[i, 'lon'] 
    y = map_polygon.at[i, 'lat']
    map_polygon.at[i, 'point'] = Point(x, y)
但是,我得到以下错误: RecursionError:比较中超出了最大递归深度

我做错了什么?数据帧的长度只有大约20k行

谢谢大家!

PS我已将整个错误信息附在下面:

map_polygon.at[i, 'point'] = Point(x, y)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 2167, in __setitem__
    return super().__setitem__(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 2118, in __setitem__
    self.obj._set_value(*key, value=value, takeable=self._takeable)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 3278, in _set_value
    self.loc[index, col] = value
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 692, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1635, in _setitem_with_indexer
    self._setitem_with_indexer_split_path(indexer, value, name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1720, in _setitem_with_indexer_split_path
    self._setitem_single_column(loc, value, pi)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1813, in _setitem_single_column
    ser._mgr = ser._mgr.setitem(indexer=(pi,), value=value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 568, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 427, in apply
    applied = getattr(b, f)(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 967, in setitem
    return self.astype(dtype).setitem(indexer, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 967, in setitem
    return self.astype(dtype).setitem(indexer, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 967, in setitem
    return self.astype(dtype).setitem(indexer, value)
  [Previous line repeated 977 more times]
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 966, in setitem
    dtype, _ = maybe_promote(np.array(value).dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/geometry/point.py", line 111, in array_interface
    if self.is_empty:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/geometry/base.py", line 699, in is_empty
    return (self._geom is None) or bool(self.impl['is_empty'](self))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/predicates.py", line 25, in __call__
    return self.fn(this._geom)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/geos.py", line 583, in errcheck_predicate
    if result == 2:
RecursionError: maximum recursion depth exceeded in comparison

添加几何体列时,循环并单独设置每个值通常是个坏主意(速度慢),因此您可能希望这样做:

map_polygon['point'] = [Point(p) for p in zip(df.lon, df.lat)]

在OP的示例中,
Point(…)
接受两个参数,这里您将一个参数作为元组。可能应该是
[点(*p)表示zip中的p(df.lon,df.lat)]
[点(lon,lat)表示zip中的lon,lat(df.lon,df.lat)]
根据shapely的文档,点采用元组或位置参数:“点构造函数采用位置坐标值或点元组参数。”,传递元组意味着更少的打字伟大的
,我被纠正了。