RecursionError:与使用熊猫时相比,超出了最大递归深度

问题描述

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

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,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,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,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,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",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)]