当前位置:   article > 正文

python 异常处理 datafram 重连_Python:在pandas datafram上使用多处理

failed to create point instance from string

很奇怪。它似乎在Python2下有效,但在Python3下无效。

这是打印输出的最小修改版本:import pandas as pd

from geopy.distance import vincenty

from itertools import combinations

import multiprocessing as mp

df = pd.DataFrame({'ser_no': [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],

'co_nm': ['aa', 'aa', 'aa', 'bb', 'bb', 'bb', 'bb', 'cc', 'cc', 'cc'],

'lat': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

'lon': [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]})

def calc_dist(x):

ret = pd.DataFrame(

[ [grp,

df.loc[c[0]].ser_no,

df.loc[c[1]].ser_no,

vincenty(df.loc[c[0], x],

df.loc[c[1], x])

]

for grp,lst in df.groupby('co_nm').groups.items()

for c in combinations(lst, 2)

],

columns=['co_nm','machineA','machineB','distance'])

print(ret)

return ret

if __name__ == '__main__':

pool = mp.Pool(processes = (mp.cpu_count() - 1))

pool.map(calc_dist, ['lat','lon'])

pool.close()

pool.join()

这是python2的输出0 aa 1 2 110.723608682 km

1 aa 1 3 221.460709525 km

2 aa 2 3 110.737100843 km

3 cc 8 9 110.827576495 km

4 cc 8 0 221.671650552 km

co_nm machineA machineB distance

5 cc 9 0 110.844074057 km

0 aa 1 2 110.575064814 km

1 aa 1 3 221.151481337 km

6 bb 4 5 110.765515243 km

2 aa 2 3 110.576416524 km

7 bb 4 6 221.5459187 km

3 cc 8 9 110.598565514 km

4 cc 8 0 221.203121352 km

8 bb 4 7 332.341640771 km

5 cc 9 0 110.604555838 km

6 bb 4 5 110.58113908 km

9 bb 5 6 110.780403457 km

7 bb 4 6 221.165643396 km

10 bb 5 7 221.576125528 km

8 bb 4 7 331.754177186 km

9 bb 5 6 110.584504316 km

10 bb 5 7 221.173038106 km

11 bb 6 7 110.795722071 km

11 bb 6 7 110.58853379 km

这是python3的堆栈痕迹"""

Traceback (most recent call last):

File "/usr/local/lib/python3.4/dist-packages/geopy/point.py", line 123, in __new__

seq = iter(arg)

TypeError: 'numpy.int64' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/lib/python3.4/multiprocessing/pool.py", line 119, in worker

result = (True, func(*args, **kwds))

File "/usr/lib/python3.4/multiprocessing/pool.py", line 44, in mapstar

return list(map(*args))

File "gps.py", line 29, in calc_dist

for grp, lst in df.groupby('co_nm').groups.items()

File "gps.py", line 30, in

for c in combinations(lst, 2)

File "/usr/local/lib/python3.4/dist-packages/geopy/distance.py", line 322, in __init__

super(vincenty, self).__init__(*args, **kwargs)

File "/usr/local/lib/python3.4/dist-packages/geopy/distance.py", line 115, in __init__

kilometers += self.measure(a, b)

File "/usr/local/lib/python3.4/dist-packages/geopy/distance.py", line 342, in measure

a, b = Point(a), Point(b)

File "/usr/local/lib/python3.4/dist-packages/geopy/point.py", line 126, in __new__

"Failed to create Point instance from %r." % (arg,)

TypeError: Failed to create Point instance from 8.

"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File "gps.py", line 38, in

pool.map(calc_dist, ['lat', 'lon'])

File "/usr/lib/python3.4/multiprocessing/pool.py", line 260, in map

return self._map_async(func, iterable, mapstar, chunksize).get()

File "/usr/lib/python3.4/multiprocessing/pool.py", line 599, in get

raise self._value

TypeError: Failed to create Point instance from 8.

我知道这不是答案,但也许有帮助。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/821596
推荐阅读
相关标签
  

闽ICP备14008679号