2

I am using Tableau Prep with Python Script, but when I am executing the Python Script, in Output Flow getting an error of "prep_int() function is not defined". prep_int() I have used in Python Script for get_output_schema function.

Python Code:

import pandas as pd
import requests
import json

df = pd.read_csv("E:/dummy.csv")
port = "8080"

target_columns = ["id"]
# target_columns = df['id']
source_columns = list(set(df.columns) - {"target"})
# print(target_columns)

target_json = json.loads(df[target_columns].to_json(orient="records"))
source_json = json.loads(df[source_columns].to_json(orient="records"))

# print(source_json)

payload = {"source": {"data": source_json}, "target": {"data": target_json}}
# print(payload)
# Run a single or mini-batch prediction
headers = {"content-type": "application/json", "accept": "application/json"}

r = requests.post(
    "http://14.141.154.146:9871/invocations", data=json.dumps(payload), headers=headers
)

# print(r.text)


def filldata():
    df = pd.DataFrame()
    ID(int)
    # fill df up with data
    # for example
    # name (string)
    # time (datetime)
    # number (int)

    return df


def get_output_schema(self):
    return pd.DataFrame({"ID": prep_int()})

  • Tableau Prep Screen with Error Message:

enter image description here

1 Answers1

1

You are nearly there!

Looking at the documentation https://help.tableau.com/current/prep/en-us/prep_scripts_TabPy.htm

You have to get the internals of your return pandas DataFrame and your def get_output_schema() to match.

You are not returning a DataFrame anywhere and the get_output_schema is also not matching that DataFrame as it does not exist.

so you would need a function

def filldata():
    df=pd.DataFrame()
    # fill df up with data
    # for example
    # name (string)
    # time (datetime)
    # number (int)

    return df

and you would need a corresponding function

def get_output_schema():
    return pd.DataFrame({
    'name' : prep_string(),
    'time' : prep_datetime(),
    'number' : prep_int()
})

then move over to tableau to point to the script and the function to call to get the data.

Paul Brennan
  • 2,638
  • 4
  • 19
  • 26
  • Still getting the error "prep_int() is not defined". adding the updated script. Please have a look. – Abhijit Samantaray Nov 19 '20 at 18:34
  • You need to put the data you want to pass back into the DataFrame df in the function filldata(). I don't have enough to go on with this question to do that for you. You just put ID(int). you need to put something like df['ID'] = * where ID is the column name and the * is the data (of type int in a series that matches to the data). * is of course a place holder for your code. – Paul Brennan Nov 19 '20 at 18:43