diff --git a/__init__.py b/__init__.py index dc92201..ca1030b 100644 --- a/__init__.py +++ b/__init__.py @@ -52,6 +52,7 @@ from .modules.pandas_wrapper.pandas_mul import PandasMul from .modules.pandas_wrapper.pandas_mul_series import PandasMulSeries from .modules.pandas_wrapper.pandas_pow import PandasPow from .modules.pandas_wrapper.pandas_pow_series import PandasPowSeries +from .modules.pandas_wrapper.pandas_rename import PandasRename from .modules.pandas_wrapper.pandas_replace import PandasReplace from .modules.pandas_wrapper.pandas_save_csv import PandasSaveCSV from .modules.pandas_wrapper.pandas_save_json import PandasSaveJSON @@ -127,6 +128,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = { "PandasMode": PandasMode, "PandasMul": PandasMul, "PandasMulSeries": PandasMulSeries, + "PandasRename": PandasRename, "PandasReplace": PandasReplace, "PandasPow": PandasPow, "PandasPowSeries": PandasPowSeries, @@ -209,6 +211,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = { "PandasMulSeries": "Pandas Mul Series", "PandasPow": "Pandas Pow", "PandasPowSeries": "Pandas Pow Series", + "PandasRename": "Pandas Rename", "PandasReplace": "Pandas Replace", "PandasSaveCSV": "Pandas Save CSV", "PandasSaveJSON": "Pandas Save JSON", diff --git a/docs/reference/node_reference.md b/docs/reference/node_reference.md index 1021df3..a787df0 100644 --- a/docs/reference/node_reference.md +++ b/docs/reference/node_reference.md @@ -105,6 +105,7 @@ | [Pandas Horizontal Concat](pandas_horizontal_concat.md) | Horizontally concatenates two pandas DataFrames. | | [Pandas Horizontal Split](pandas_horizontal_split.md) | Horizontally splits a Pandas DataFrame into two pandas DataFrames. | | [Pandas Join](pandas_join.md) | Merges two pandas DataFrames based on a common column. | +| [Pandas Rename](pandas_rename.md) | Renames an index (row label) or a column label. | | [Pandas Sort](pandas_sort.md) | Sorts a pandas DataFrame by a specified column. | | [Pandas Vertical Concat](pandas_vertical_concat.md) | Vertically concatenates two pandas DataFrames. | | [Pandas Vertical Split](pandas_vertical_split.md) | Vertically splits a Pandas DataFrame into two pandas DataFrames. | diff --git a/docs/reference/pandas_rename.md b/docs/reference/pandas_rename.md new file mode 100644 index 0000000..70f119f --- /dev/null +++ b/docs/reference/pandas_rename.md @@ -0,0 +1,20 @@ +# Pandas Rename +Renames an index (row label) or a column label. + +## Input +| Name | Data type | +|---|---| +| dataframe | DataFrame | +| axis | DataFrame | +| current_label | String | +| new_label | | + +## Output +| Data type | +|---| +| DataFrame | + +
+Category: Transformation + +ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved. diff --git a/modules/pandas_wrapper/pandas_as_int.py b/modules/pandas_wrapper/pandas_as_int.py index 5ba26da..1eeb94d 100644 --- a/modules/pandas_wrapper/pandas_as_int.py +++ b/modules/pandas_wrapper/pandas_as_int.py @@ -40,4 +40,4 @@ class PandasAsInt: """ # Convert all values to integer, non-numeric values become NaN df = dataframe.apply(pd.to_numeric, errors='coerce').astype('Int64') - return (df.to_json(),) + return (df,) diff --git a/modules/pandas_wrapper/pandas_rename.py b/modules/pandas_wrapper/pandas_rename.py new file mode 100644 index 0000000..bfe2fdc --- /dev/null +++ b/modules/pandas_wrapper/pandas_rename.py @@ -0,0 +1,52 @@ +from typing import Any, Dict +import pandas as pd + + +class PandasRename: + """ + Renames an index (row label) or a column label. + + category: Transformation + """ + + @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": ("DATAFRAME", {}), + "axis": (("columns", "index"),), + "current_label": ("STRING", {"default": ""}), + "new_label": ("STRING", {"default": ""}), + } + } + + RETURN_TYPES: tuple = ("DATAFRAME",) + FUNCTION: str = "f" + CATEGORY: str = "Data Analysis" + + def f(self, dataframe: pd.DataFrame, axis: str, current_label: str, new_label: str) -> tuple: + """ + Renames an index (row label) or a column label. + + Args: + dataframe (DataFrame): The DataFrame. + axis (str): Index (row label) or column label. + current_label (str): The current label. + new_label (str): The new label. + + Returns: + tuple: A tuple containing a JSON string of the renamed DataFrame. + """ + if axis=="index": + df_out = dataframe.rename(index={current_label:new_label}) + elif axis=="columns": + df_out = dataframe.rename(columns={current_label:new_label}) + else: + raise ValueError("Invalid axis. Only index or columns are supported.") + return (df_out,) diff --git a/pyproject.toml b/pyproject.toml index 3e09ef0..49ee18a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "data-analysis" description = "Data analysis custom modules for ComfyUI - Use Pandas & Matplotlib within ComfyUI" -version = "1.1.8" +version = "1.1.9" license = {file = "LICENSE"} [project.urls]