Added CDA JSON Create and Pandas Iloc Rows DataFrame nodes.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from typing import Any, Dict
|
||||
import json
|
||||
|
||||
class CDAJSONCreate:
|
||||
"""
|
||||
CDAJSONCreate:
|
||||
A class for creating a serialized JSON object using values entered in the text field.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `create` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"data": ("STRING", {"multiline": True}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("STRING",)
|
||||
FUNCTION: str = "create"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def create(self, data: str) -> tuple:
|
||||
"""
|
||||
creating a serialized JSON object using values entered in the text field.
|
||||
|
||||
Args:
|
||||
data (str): String value in the text field.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string representation of the DataFrame.
|
||||
"""
|
||||
# Convert to JSON
|
||||
json_obj = json.loads(data)
|
||||
|
||||
# Serialize to a string
|
||||
result_jsons = json.dumps(json_obj)
|
||||
|
||||
return (result_jsons,)
|
||||
|
||||
@@ -5,7 +5,7 @@ import pandas as pd
|
||||
|
||||
class PandasCreate:
|
||||
"""
|
||||
PandasLoadCSV:
|
||||
PandasCreate:
|
||||
A class for creating a pandas DataFrame using values entered in the text field.
|
||||
"""
|
||||
|
||||
@@ -32,7 +32,7 @@ class PandasCreate:
|
||||
Create a pandas DataFrame using values entered in the text field and converts it to a JSON string.
|
||||
|
||||
Args:
|
||||
file_path (str): The path to the CSV file.
|
||||
data (str): String value in the text field.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string representation of the DataFrame.
|
||||
|
||||
@@ -5,7 +5,7 @@ import pandas as pd
|
||||
|
||||
class PandasCreateWithIndex:
|
||||
"""
|
||||
PandasLoadCSV:
|
||||
PandasCreateWithIndex:
|
||||
A class for creating a pandas DataFrame using values entered in the text field.
|
||||
Input data is assume to have an index.
|
||||
"""
|
||||
@@ -35,7 +35,8 @@ class PandasCreateWithIndex:
|
||||
Input data is assume to have an index.
|
||||
|
||||
Args:
|
||||
file_path (str): The path to the CSV file.
|
||||
data (str): String value in the text field.
|
||||
index_col (int): Position of the index column.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string representation of the DataFrame.
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
from typing import Any, Dict
|
||||
from io import StringIO
|
||||
import json
|
||||
import pandas as pd
|
||||
from .utils import series_to_jsons
|
||||
|
||||
class PandasIlocRowsDataFrame:
|
||||
"""
|
||||
PandasIlocRowsDataFrame:
|
||||
A class for selecting rows from a pandas DataFrame and returning it as a DataFrame.
|
||||
Rows are specified by the list of integer positions.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `select_rows` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"dataframe_json": ("STRING", {"multiline": True}),
|
||||
"row_int_pos_list_json": ("STRING", {"multiline": False})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("STRING",)
|
||||
FUNCTION: str = "select_rows"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def select_rows(self, dataframe_json: str,
|
||||
row_int_pos_list_json: str) -> tuple:
|
||||
"""
|
||||
Select rows from a pandas DataFrame and returning it as a DataFrame.
|
||||
Rows are specified by the list of integer positions and returned as
|
||||
the JSON serialized DataFrame object.
|
||||
|
||||
Args:
|
||||
dataframe_json (str): A JSON string representation of the DataFrame.
|
||||
row_int_pos_list_json (str): A list containing row integer positions.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the value of the row in string. The string is
|
||||
a JSON string containing the serialized Pandas Series object.
|
||||
"""
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(StringIO(dataframe_json))
|
||||
row_int_pos_list = json.loads(row_int_pos_list_json)
|
||||
df2 = df.iloc[row_int_pos_list]
|
||||
return (df2.to_json(),)
|
||||
Reference in New Issue
Block a user