# After execute code in knime python script node got error like knime user error: output table'0' must be of type knime.api.Table or knime.api., Batch output table but got None. Did you assign the output table.

**URL:** <https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075>\
**Category:** KNIME Analytics Platform\
**Created:** [March 24, 2024, 6:26pm UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075 "2024-03-24T18:26:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohit\_208](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Rohit\_208](https://forum.knime.com/u/Rohit_208)\
**Post date:** [March 24, 2024, 6:26pm UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075/1 "2024-03-24T18:26:59Z")

</div>

Hello Knime users,  
this is my preprocessing code. but after execute code in python script node of knime. then show error. please help me  
KnimeUserError: Output table ‘0’ must be of type knime.api.Table or knime.api.BatchOutputTable, but got None. Did you assign the output table?

import pandas as pd  
import numpy as np  
from sklearn.preprocessing import LabelEncoder, OneHotEncoder  
import [knime.scripting.io](http://knime.scripting.io) as knio

class TimeSeriesPreprocessor:  
def **init** (self, input\_tables):  
self.input\_tables = input\_tables

```
def drop_columns(self):
    columns_to_drop = [
        "[Time Freeze].[Time Freeze].[Freeze Type].[MEMBER_CAPTION]",
        "[Time Freeze].[Time Freeze].[FreezeTime - FinYear].[MEMBER_CAPTION]",
        "[Time Freeze].[Time Freeze].[FreezeTime - TimeRef Level1].[MEMBER_CAPTION]"
    ]
    datetime_column = "[Time Freeze].[Time Freeze].[FreezeTime - TimeReference].[MEMBER_CAPTION]"
    try:
        self.input_tables[datetime_column] = pd.to_datetime(self.input_tables[datetime_column])
    except pd.errors.ParserError:
        print(f"Warning: Failed to automatically parse '{datetime_column}' to datetime format.")
    self.input_tables = self.input_tables.drop(columns=columns_to_drop, axis=1) # Corrected the drop method

def convert_to_numeric(self):
    for column in self.input_tables.columns:
        if self.input_tables[column].dtype == 'object' and column != "[Time Freeze].[Time Freeze].[FreezeTime - TimeReference].[MEMBER_CAPTION]":
            self.input_tables[column] = pd.to_numeric(self.input_tables[column], errors='coerce')

def convert_numeric_objects(self):
    for column in self.input_tables.columns:
        if self.input_tables[column].dtype == 'object' and column != "[Time Freeze].[Time Freeze].[FreezeTime - TimeReference].[MEMBER_CAPTION]":
            self.input_tables[column] = self.input_tables[column].astype(float)

def encode_categorical(self):
    for column in self.input_tables.columns:
        if self.input_tables[column].dtype == 'object' and column != "[Time Freeze].[Time Freeze].[FreezeTime - TimeReference].[MEMBER_CAPTION]":
            if len(self.input_tables[column].unique()) <= 10:
                le = LabelEncoder()
                self.input_tables[column] = le.fit_transform(self.input_tables[column])
            else:
                ohe = OneHotEncoder(handle_unknown='ignore', sparse=False)
                encoded_data = pd.DataFrame(ohe.fit_transform(self.input_tables[[column]]))
                self.input_tables = pd.concat([self.input_tables, encoded_data], axis=1)
                self.input_tables.drop(column, axis=1, inplace=True)

def fill_missing_values(self):
    self.input_tables = self.input_tables.interpolate(method='linear')

def handle_outliers(self):
    for column in self.input_tables.columns:
        if self.input_tables[column].dtype != 'datetime64[ns]' and column != "[Time Freeze].[Time Freeze].[FreezeTime - TimeReference].[MEMBER_CAPTION]":
            q1 = self.input_tables[column].quantile(0.25)
            q3 = self.input_tables[column].quantile(0.75)
            iqr = q3 - q1
            lower_bound = q1 - 1.5 * iqr
            upper_bound = q3 + 1.5 * iqr
            self.input_tables[column] = np.where(self.input_tables[column] < lower_bound, lower_bound, self.input_tables[column])
            self.input_tables[column] = np.where(self.input_tables[column] > upper_bound, upper_bound, self.input_tables[column])

def preprocess_data(self):
    self.drop_columns()
    self.convert_to_numeric()
    self.convert_numeric_objects()
    self.encode_categorical()
    self.fill_missing_values()
    self.handle_outliers()
    return self.input_tables # Return the preprocessed data

```

def main():  
input\_tables = knio.input\_tables[0].to\_pandas()  
preprocessor = TimeSeriesPreprocessor(input\_tables)  
preprocessed\_data = preprocessor.preprocess\_data()  
output\_tables = knio.Table.from\_pandas(preprocessed\_data)  
knio.output\_tables[0] = output\_tables

if **name** == “ **main** ”:  
main()

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [March 24, 2024, 7:20pm UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075/2 "2024-03-24T19:20:36Z")

</div>

@Rohit_208 welcome to the KNIME forum. Maybe you can check if the data in and out is correct.

> [@Help to use Python do insert variable in SQL code](https://forum.knime.com/t/help-to-use-python-do-insert-variable-in-sql-code/63058/2):
>
> @Aldemir you can export data from within the Python node as Flow Variables or as a dataframe (from a pandas dataframe). The code would look something like this: import knime.scripting.io as knio import numpy as np import pandas as pd # bring your string to a Flow Variable knio.flow\_variables['var\_txt\_cbi'] = txt\_cbi # bring your content to a pandas data frame output\_table = pd.DataFrame({'column\_txt\_cbi': [txt\_cbi]}) # bring the dataframe back to KNIME knio.output\_tables[0] = knio.Table.f…

> [@Python scripting node data input](https://forum.knime.com/t/python-scripting-node-data-input/63962/2):
>
> @Bob_Nisbet the in and out syntax would look like this for pandas dataframes. import knime.scripting.io as knio import numpy as np import pandas as pd # data from KNIME to Python (pandas) df = knio.input\_tables[0].to\_pandas() # data from python (pandas) to KNIME knio.output\_tables[0] = knio.Table.from\_pandas(df) KNIME would work internally with Arrow format [https://docs.knime.com/latest/python\_installation\_guide/index.html#usage-examples](https://docs.knime.com/latest/python_installation_guide/index.html#usage-examples)

Also maybe you can provide the whole code in a readable form as a separate file or preformated.

---

<div class="post-metadata">

**Author:** ![Rohit\_208](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Rohit\_208](https://forum.knime.com/u/Rohit_208)\
**Post date:** [March 25, 2024, 5:21am UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075/3 "2024-03-25T05:21:31Z")

</div>

@mlauber71 see first i created micro sql server node then this node connected with db connector node. In this node insert query as per requirement fetch data then connected with python script node. So if i am opening node then there left side i can see all fetch data. please see in below image you will get idea.

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/4/1/41ba87d0bf6df0d83a1e71d6743b1fdfb2eaa7e9.png)

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [March 25, 2024, 6:18am UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075/4 "2024-03-25T06:18:53Z")

</div>

@Rohit_208 you will have to make sure that at the end you have an (Arrow) output table. That seems to be missing. Instead you have a variable with the name of the output table. Maybe check the examples I linked.

---

<div class="post-metadata">

**Author:** ![Rohit\_208](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Rohit\_208](https://forum.knime.com/u/Rohit_208)\
**Post date:** [March 25, 2024, 5:37pm UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075/5 "2024-03-25T17:37:15Z")

</div>

@mlauber71 thanks it’s working

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [April 1, 2024, 5:37pm UTC](https://forum.knime.com/t/after-execute-code-in-knime-python-script-node-got-error-like-knime-user-error-output-table0-must-be-of-type-knime-api-table-or-knime-api-batch-output-table-but-got-none-did-you-assign-the-output-table/78075/6 "2024-04-01T17:37:35Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
