Hello All,
This is my first question here.
I have a python script which is giving me the required output. However the moment I run it in tableau (using SCRIPT_REAL) it says TypeError : Object of type DataFrame is not JSON serializable. the script is attached below:
SCRIPT_REAL("
import quants_core
import numpy as np
import pandas as pd
from datetime import datetime
from quants_core.stats import get_stats_all
coll = pd.read_excel(r'C:\Users\ntharani\Desktop\Data.xlsx', index_col=None, na_values=['NA'], usecols = 'A,D')
print(coll.head())
coll.set_index('Dates', inplace=True)
coll.index = pd.to_datetime(coll.index, format='%Y-%m-%dT%H:%M:%S.%fZ')
#print(coll) # Returns are read and stored here
yy = get_stats_all(coll)
zz = yy.transpose()
zz = yy.values.T.flatten()
print(zz)
return zz.values.tolist()
",[Return])
The get_stats_all function returns a DataFrame with 1 row and 22 columns, The output is getting displayed on command line but somehow it is not getting displayed in Tableau views.
Any suggestions or improvements are appreciated. Thanks in advance.
PS: I cannot attach workbook due to confidentiality issues.
Message was edited by: Neha Tharani
Hi Neha,
In your Python code you'd need to turn the data frame into a string of numbers (and potentially text) with defined separators, maybe something like:
1.0~23~~3.14~~~red
Then you'd need a set of N calculations (four in this example) to get each value, here's a screenshot:
As noted in the comment the formula is more complicated because (as of at least Tableau v2018.3) SPLIT, FINDNTH, and REGEX_EXTRACT_NTH aren't supported by table calculations (and the SCRIPT functions that Python is executed in are table calculations), vote up if you'd like to get that.
Note that if the Python script is returning a changing number of variables in each run there's no automatic way to parse out into a variable number of calculated fields - you'll need to build as many calculations as you expect to have variables.
I've attached a v10.5 workbook I used to make the screenshot above.
Jonathan
PS: I know this is really painful, I had a conversation with a Tableau developer last month and I know they are looking into this. I'll also forward this thread as an example.