Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dask apply: AttributeError: 'DataFrame' object has no attribute 'name'

Tags:

python

dask

I have a dataframe of params and apply a function to each row. this function is essentially a couple of sql_queries and simple calculations on the result.

I am trying to leverage Dask's multiprocessing while keeping structure and ~ interface. The example below works and indeed has a significant boost:

def get_metrics(row):

    record = {'areaName': row['name'],
              'areaType': row.area_type,
              'borough': row.Borough,
              'fullDate': row['start'],
              'yearMonth': row['start'],
              }


    Q = Qsi.format(unittypes=At,
                   start_date=row['start'],
                   end_date=row['end'],
                   freq='Q',
                   area_ids=row['descendent_ids'])

    sales = _get_DF(Q)
    record['salesInventory'] = len(sales)
    record['medianAskingPrice'] = sales.price.median()
    R.append(record)

R = []
x = ddf.map_partition(lambda x: x.apply(_metric, axis=1), meta={'result': None})
    x.compute()

result2 = pd.DataFrame(R)

However, when I try to use .apply method instead (see below), it throws me 'DataFrame' object has no attribute 'name'...

R = list()
y = ddf.apply(_metrics, axis=1, meta={'result': None})

Yet, ddf.head() shows that there is a name column in the dataframe

like image 937
Philipp_Kats Avatar asked Oct 13 '17 19:10

Philipp_Kats


1 Answers

If the output of your _metric function is a Series, maybe you should use meta=('your series's columns name','output's dtype')

This worked for me.

like image 145
Cherrymelon Avatar answered Oct 02 '22 15:10

Cherrymelon