Pankaj Kaundal
Pankaj Kaundal

Reputation: 1022

Unable to retrieve results for field [{0}] TabPy error while using a python sql function in the flow

I am trying to run SQL queries within a defined function. I want a pandas data frame as an output but I get a list though the data seems correct. Here's my code:

    import pandas as pd
    from pandasql import sqldf
    import pandasql as psql

    cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
                'Price': [22000,25000,27000,35000]}
    df = pd.DataFrame(cars)
    
    
    def test_query(df):
        df = pd.DataFrame(df)
        q = """select * from df"""
    
        df2 = psql.sqldf(q, locals())
        return df2

   test_query(df)

This returns a fine pandas data frame that I actually need.

Now where the problem persists is that when I run this function on my data in Tableau Prep Builder the error I am getting is

Unable to retrieve results for field [{0}]

I looked up for this error and found that "you need to use a python command to return something where something is a list containing the appropriate return type. Otherwise, the values may exist in python but Tableau can't see them"

thus I used

 ReturnValues = []
 ReturnValues.append(df2)

still, I get the same error as Unable to retrieve results for field [{0}]

What could be the reason here.

Upvotes: 0

Views: 1056

Answers (2)

Ronak SHAH
Ronak SHAH

Reputation: 181

There are 3 problems with your code.

  1. You are calling test_query function in the scope of definition of test_query function itself.

  2. to the function you are passing dataframe object itself. So you don't need to again create a new dataframe object from it.

  3. in the test_query function, you are creating ReturnValues variable of list datatype and then appending your query result to it. So instead of that, you can directly return df2

Upvotes: 1

Sai
Sai

Reputation: 1700

It is returning list of dataframes, So just pick the first element of the list, you're done.

print(test_query(df)[0])

Upvotes: 1

Related Questions