Skip to content
Advertisement

SQL retrieval: Empty Dataframe in IDLE or Visual Studio Code but populated Dataframe in Jupyter Notes

I am not a good python coder (beginner) so apologies if the code isn’t up to pythonista’s snuff! Bit of a weird situation and I can not figure this out. I have been wracking my brains trying to fix it out but can’t seem to be able to. I am sure it’s a really simple fix I am overlooking…

The “allData” Dataframe is pulling data from an SQL database. From that I am extracing a number of records and appending to the “managerListImput” empty dataframe. I am iterating for a number of records specified and looking up the details for each one before appending. It works in Jupyter notes and I get the desired output but when I run it in IDLE or Visual Studio Code I get an empty dataframe. Very frustrating. Hopefully someon can solve it…. Thanks in advance

Here’s the code:

def selected():
   managerReturns = pd.DataFrame(columns = ['hName','Returns'])
   managerCount = int(input("how many MANAGERS do you want to use?  Enter a quantity: "))
   i=0
   while i < managerCount:        
       managerListData = allData[['hID','hName','Min_Date','Max_Date']]
       managerListFind = input('Enter partial or whole MANAGER name: ')
       managerList = managerListData[managerListData['hName'].str.contains(managerListFind, case=False)]
       managerList = managerList.drop_duplicates(subset=['hID'])
       managerList = managerList.set_index('hID')
       print(managerList)
       managerID = input('Pick/Type in an MANAGER list ID ("hID") From the List Above: ')
       managerName = managerListData.query('hID ==' + str(managerID))['hName']
       managerName = managerName.iloc[0] # otherwise you get a list of the same names
       manager = allData.query('hID == @managerID')

       manager = manager.filter(['hName','Returns'])  
       managerReturns = managerReturns.append(manager)
    
       i += 1

   managerReturns = managerReturns.groupby([managerReturns.index, 'hName'])['Returns'].mean().unstack()
   return managerReturns   

test31 = selected()
print(test31)

Output in IDLE or Visual Studio Code:

Empty DataFrame
Columns: []
Index: []

Output (in Jupyter Notes) i.e. the output I want!::

Date        Manager1    Manager2
4/1/2010    NaN         0.1459
5/1/2010    NaN         0.0049
6/1/2010    NaN        -0.0206
7/1/2010    NaN        -0.0416
8/1/2010    NaN         0.4537
...         ...         ...
7/1/2021    0.0056      0.0047
8/1/2021    0.0185     -0.0086
9/1/2021   -0.0269      0.0207
10/1/2021   0.0261     -0.0080
11/1/2021  -0.0256      0.0228

Advertisement

Answer

The solution was simple: the managerID has to be turned into an integer, so I added this:

managerID = int(managerID)

I guess Jupyter Notes is smart enough to recognize it as a number.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement