Using this implementation of parallelization raises an ImportError: cannot import name 'Parallel' from 'multiprocessing'
The following code tries parallelization with the "denominator" function and should give me the sum of the fields "basalareap","basalareas","basalaread" in a new column. When I import the whole library viafrom multiprocessing import *
The process start but comes to no end.
What is wrong with my syntax?
import numpy as np
from multiprocessing import cpu_count, Parallel
import pandas as pd
#Some example dataframe
np.random.seed(4)
layer = pd.DataFrame(np.random.randint(0,25,size=(10, 4)),
columns=list(['basalareap','notofinterest', 'basalareas', 'basalaread']))
###Filter Fields by selecting columns of interest
fields = ["basalareap","basalareas","basalaread"]
#In reality data is a geodatframe, it would be:
#layer = layer[fields+['geometry']]
#but here:
layer = fields
data = layer
def denom():
data['denominator'] = data[["basalareap","basalareas","basalaread"]].sum(axis=1)
cores = cpu_count()
partitions = cores
def parallelize(data,func):
data_split = np.array_split(data,partitions)
pool = Pool(cores)
data = pd.concat(pool.map(func, data_split))
pool.close()
pool.join()
return data
data = parallelize(data,denom)
I'm using Windows 10 and Python 3.7.4
parallelize
function as requiring a function as a second parameter. You then pass it aSeries
, specifically the result of a sum over the"denominator"
column. I'm not sure what your goal is.