2
$\begingroup$

I have my code where i want to apply a function on and overwirte the inputs based on the return data, here is my apply section

panda_dataframe[[
    'distributor-stock-cost-price-real-distributor',
    'distributor-stock-cost-price-stock',
    'distributor-stock-cost-price-expected',
    'distributor-stock-cost-price-country',
    'distributor-stock-cost-price-real-distributor',
    'distributor-stock-cost-price-real-stock',
    'distributor-stock-cost-price-real-expected',
    'distributor-stock-cost-price-real-country'
]] = panda_dataframe[['uuid']].apply(lambda x:__getDistributorStockData(x))

And then i got my function i apply to my dataframe.

def __getDistributorStockData(x):
    in_product_uuid = x


    out_cost_price_dist = None
    out_cost_price_stock = None
    out_cost_price_expected = None
    out_cost_price_country = None
    out_cost_price_real_dist = None
    out_cost_price_real_stock = None
    out_cost_price_real_expected = None
    out_cost_price_real_country = None


    try:
        product_data = store_remote_stock_dataframe.get_group(in_product_uuid)

        product_data_onstock = product_data.loc[product_data['Stock'] > 0, ['Stock', 'CostPriceReal', 'CostPrice', 'Expected', 'DistributorUUID', 'Country']]
        product_data_outstock = product_data.loc[product_data['Stock'] <= 0, ['Stock', 'CostPriceReal', 'CostPrice', 'Expected', 'DistributorUUID', 'Country']]

        if len(product_data_onstock) > 0:
            stock_cost_price = product_data_onstock.sort_values(by=['CostPrice'], ascending=True).iloc[0,:]
            stock_cost_real_price = product_data_onstock.sort_values(by=['CostPriceReal'], ascending=True).iloc[0,:]

            out_cost_price_dist = stock_cost_price['DistributorUUID']
            out_cost_price_stock = stock_cost_price['Stock']
            out_cost_price_expected =  stock_cost_price['Expected']
            out_cost_price_country =  stock_cost_price['Country']
            out_cost_price_real_dist = stock_cost_real_price['DistributorUUID']
            out_cost_price_real_stock = stock_cost_real_price['Stock']
            out_cost_price_real_expected = stock_cost_real_price['Expected']
            out_cost_price_real_country = stock_cost_real_price['Country']

        elif len(product_data_outstock) > 0:
            stock_cost_price = product_data_outstock.sort_values(by=['CostPrice'], ascending=True).iloc[0,:]
            stock_cost_real_price = product_data_outstock.sort_values(by=['CostPriceReal'], ascending=True).iloc[0,:]

            out_cost_price_dist = stock_cost_price['DistributorUUID']
            out_cost_price_stock = stock_cost_price['Stock']
            out_cost_price_expected =  stock_cost_price['Expected']
            out_cost_price_country =  stock_cost_price['Country']
            out_cost_price_real_dist = stock_cost_real_price['DistributorUUID']
            out_cost_price_real_stock = stock_cost_real_price['Stock']
            out_cost_price_real_expected = stock_cost_real_price['Expected']
            out_cost_price_real_country = stock_cost_real_price['Country']
        else:
            stock_cost_price = None
            stock_cost_real_price = None
    except:
        stock_cost_price = None
        stock_cost_real_price = None

    return [
        out_cost_price_dist,
        out_cost_price_stock,
        out_cost_price_expected,
        out_cost_price_country,
        out_cost_price_real_dist,
        out_cost_price_real_stock,
        out_cost_price_real_expected,
        out_cost_price_real_country
    ]

My big issue is everything run fin, i have try it out on a single row, and its work fine, but its return inside a array, and now i want to apply it on every single row but i got this error.

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    pricerule_calculate_run = PriceruleCalculate.run(method='business')
  File ".....", line 940, in run
    __setupProducts(productUUID)
  File ".....", line 507, in __setupProducts
    ]] = panda_dataframe[['uuid']].apply(lambda x:__getDistributorStockData(x), axis=1)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2514, in __setitem__
    self._setitem_array(key, value)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2544, in _setitem_array
    self.loc._setitem_with_indexer((slice(None), indexer), value)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py", line 599, in _setitem_with_indexer
    raise ValueError('Must have equal len keys and value '
ValueError: Must have equal len keys and value when setting with an iterable

can eny how help me here and find my mistake, i'm still very new for Pandas and i'm optimize my data handler right away.

$\endgroup$
4
  • $\begingroup$ Your error line no 12 - pricerule_calculate_run = PriceruleCalculate.run(method='business'), where is this line in the code you have posted? $\endgroup$ – Ankit Seth Mar 15 '18 at 12:12
  • $\begingroup$ no, the erros is not in this line, the line is just a function when i call the error is the code i post, mabey in my return or somthing but i'm not 100% sure. $\endgroup$ – ParisNakitaKejser Mar 15 '18 at 12:19
  • $\begingroup$ Can you post complete code? Or, atleast first 15 lines of your code. It's hard to find error here. $\endgroup$ – Ankit Seth Mar 15 '18 at 12:35
  • $\begingroup$ pricerule_calculate_run = PriceruleCalculate.run(method='business') this what you mean, and its still have issue, you look the wrong place its nothing to do with my start function its only the function there trigger the error, but the error its locate to my panda apple part. $\endgroup$ – ParisNakitaKejser Mar 15 '18 at 12:38
6
$\begingroup$

I found the issue, I need to return a pd.Series()

return pd.Series([
    out_cost_price_dist,
    out_cost_price_stock,
    out_cost_price_expected,
    out_cost_price_country,
    out_cost_price_real_dist,
    out_cost_price_real_stock,
    out_cost_price_real_expected,
    out_cost_price_real_country
])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.