Added Pandas Create and Pandas Create with Index nodes to create a DataFrame from text

This commit is contained in:
HowToSD
2025-02-14 14:25:59 -08:00
parent 541314d7ad
commit 7ee844832e
8 changed files with 117 additions and 6 deletions
+46
View File
@@ -0,0 +1,46 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
class PandasCreate:
"""
PandasLoadCSV:
A class for creating a pandas DataFrame 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:
"""
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.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
"""
# Read CSV file into DataFrame
df = pd.read_csv(StringIO(data))
# Convert DataFrame to JSON
result_json = df.to_json()
return (result_json,)
@@ -0,0 +1,49 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
class PandasCreateWithIndex:
"""
PandasLoadCSV:
A class for creating a pandas DataFrame using values entered in the text field.
Input data is assume to have an index.
"""
@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}),
"index_col": ("INT", {"default": 0, "min": 0, "max": 10000})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "create"
CATEGORY: str = "Data Analysis"
def create(self, data: str, index_col: int=0) -> tuple:
"""
Create a pandas DataFrame using values entered in the text field and converts it to a JSON string.
Input data is assume to have an index.
Args:
file_path (str): The path to the CSV file.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
"""
# Read CSV file into DataFrame
df = pd.read_csv(StringIO(data), index_col=index_col)
# Convert DataFrame to JSON
result_json = df.to_json()
return (result_json,)