Added Pandas Count, Max, Mean, Median, Min, Mode, Std, Sum, Var, Show Series nodes.

This commit is contained in:
HowToSD
2025-02-17 01:49:46 -08:00
parent 8a59b2f5c6
commit d3bdcace9e
15 changed files with 573 additions and 2 deletions
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasCount:
"""
PandasCount:
A class for computing the count of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with count for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.count()),)
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasMax:
"""
PandasMin:
A class for computing the max of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with max for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.max()),)
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasMean:
"""
PandasMin:
A class for computing the mean of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with mean for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.mean()),)
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasMedian:
"""
PandasMin:
A class for computing the median of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with median for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.median()),)
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasMin:
"""
PandasMin:
A class for computing the min of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with min for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.min()),)
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
class PandasMode:
"""
PandasMode:
A class for computing the mode of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a DataFrame with mode for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the DataFrame.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (df.mode().to_json(),)
@@ -0,0 +1,56 @@
"""
This code is based on
https://github.com/pythongosssss/ComfyUI-Custom-Scripts/blob/main/py/show_text.py
See credit/credit.md for the full license.
"""
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
from .utils import jsons_to_series
class PandasShowSeries:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"series_json": ("STRING", {"forceInput": True}),
},
"hidden": {
"unique_id": "UNIQUE_ID",
"extra_pnginfo": "EXTRA_PNGINFO",
},
}
INPUT_IS_LIST = True
RETURN_TYPES = ("STRING",)
FUNCTION = "notify"
OUTPUT_NODE = True
OUTPUT_IS_LIST = (True,)
CATEGORY = "Data Analysis"
def notify(self, series_json, unique_id=None, extra_pnginfo=None):
text = None
if unique_id is not None and extra_pnginfo is not None:
if not isinstance(extra_pnginfo, list):
print("Error: extra_pnginfo is not a list")
elif (
not isinstance(extra_pnginfo[0], dict)
or "workflow" not in extra_pnginfo[0]
):
print("Error: extra_pnginfo[0] is not a dict or missing 'workflow' key")
else:
# Deserialize JSON string to DataFrame
series = jsons_to_series(series_json[0])
text = [series_to_jsons(series)] # wrap in list
workflow = extra_pnginfo[0]["workflow"]
node = next(
(x for x in workflow["nodes"] if str(x["id"]) == str(unique_id[0])),
None,
)
if node:
node["widgets_values"] = [text]
else:
print("unique_id or extra_pnginfo is None.")
return {"ui": {"text": text}, "result": (text,)}
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasStd:
"""
PandasStd:
A class for computing the standard deviation of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with standard deviation for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.std()),)
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasSum:
"""
PandasSum:
A class for computing the sum of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with sum for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.sum()),)
+42
View File
@@ -0,0 +1,42 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
from .utils import series_to_jsons
class PandasVar:
"""
PandasVar:
A class for computing the variance of a pandas DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True})
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe_json: str) -> tuple:
"""
Returns a Series with variance for each column.
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
Returns:
tuple: A tuple containing a JSON string for the Series.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
return (series_to_jsons(df.var()),)