Added Pandas Rename node to rename column label or row label (index). Fixed Pandas As Int node's output which was broken due to v1.1.0 refactoring.

This commit is contained in:
HowToSD
2025-02-20 00:12:59 -08:00
parent 7597dcedfd
commit 31049725cf
6 changed files with 78 additions and 2 deletions
+1 -1
View File
@@ -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,)
+52
View File
@@ -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,)