Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Need Help With Python Code Error

bala14_07
7 - Meteor

 

Hello All

 

I am trying to convert a python code into alteryx workflow. In the last bit where for loop is used, I have put the looping part of the code in Python Component. The code is working well in Spyder and I am getting the output

 

#Package.installPackages(['pandas','numpy'])
from ayx import Alteryx
import pandas as pd
import numpy as np
from datetime import datetime

 

master_df=Alteryx.read("#1")
calendars_df=Alteryx.read("#2")
#new_df=Alteryx.read("#3")

new_df = pd.DataFrame(columns=['Parcel','Date','Volume'])
x = 0

for i, row in master_df.iterrows():

cal_name = master_df['Cal'][i]
d_r = pd.date_range(master_df['start'][i], master_df['end'][i])
price_dates = d_r[~d_r.isin(calendars_df[cal_name][calendars_df[cal_name].isin(d_r)])]
barrel_v = ((master_df['BBL VOL'][i])/(price_dates.size))/1000

if master_df['Buy/sell'][i] == "S":
barrel_v = barrel_v*-1
for j in range(price_dates.size):
new_df.loc[x,'Date'] = price_dates[j]
new_df.loc[x,'Volume'] = barrel_v
new_df.loc[x,'Parcel'] = master_df['Parcel'][i]
x = x+1

master_DF = new_df.merge(master_df, how='left', on='Parcel')
Alteryx.write(master_DF,1)

 

This is below error which I am getting

 

KeyError                                  Traceback (most recent call last)

<ipython-input-2-3ba32150fe76> in <module>

     13     barrel_v    = ((master_df['BBL VOL'][i])/(price_dates.size))/1000

     14

---> 15     if master_df['Buy/sell'][i] == "S":

     16         barrel_v = barrel_v*-1

     17     for j in range(price_dates.size😞

 

c:\users\bala.ramakrishnan\appdata\local\alteryx\bin\miniconda3\envs\jupytertool_venv\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)

   2925             if self.columns.nlevels > 1:

   2926                 return self._getitem_multilevel(key)

-> 2927             indexer = self.columns.get_loc(key)

   2928             if is_integer(indexer😞

   2929                 indexer = [indexer]

 

c:\users\bala.ramakrishnan\appdata\local\alteryx\bin\miniconda3\envs\jupytertool_venv\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)

   2657                 return self._engine.get_loc(key)

   2658             except KeyError:

-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))

   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

   2661         if indexer.ndim > 1 or indexer.size > 1:

 

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

 

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

 

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

 

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

 

KeyError: 'Buy/sell

 

 

3 REPLIES 3
PhilipMannering
16 - Nebula
16 - Nebula

First guess is that 'Buy/sell' is not a column name. Is the case correct? Maybe it's Buy/Sell...

atcodedog05
22 - Nova
22 - Nova

Hi @bala14_07 

 

I agree with @PhilipMannering/.

 

If you provide a sample data you are using. We can check it out.

bala14_07
7 - Meteor

My dad how did I miss that. Thanks Philip

Labels