Added Pandas Series To DataFrame and Pandas Transpose nodes.

This commit is contained in:
HowToSD
2025-02-20 01:55:21 -08:00
parent a0efd91f1e
commit c076e42c15
9 changed files with 132 additions and 3 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ import pandas as pd
class PandasRename:
"""
Renames an index (row label) or a column label.
Currently, renaming only works for string labels. Renaming of integer labels is not supported.
Note below node parameter description is incomplete due to a reference generator's bug:
* axis takes columns or index.
@@ -0,0 +1,46 @@
from typing import Any, Dict
import pandas as pd
class PandasSeriesToDataFrame:
"""
Converts a Pandas Series to a DataFrame.
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": {
"series": ("PDSERIES", {}),
"label": ("STRING", {"default": ""}),
"transpose": ("BOOLEAN", {"default": True})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, series: pd.Series, label: str, transpose: bool) -> tuple:
"""
Converts a Pandas Series to a DataFrame.
Args:
series (Series): The Series.
label (str): The label for the Series in the DataFrame.
transpose (bool): Whether to transpose the DataFrame after conversion.
If True, Series will become a row. Otherwise it will become a column.
Returns:
tuple: A tuple containing the DataFrame.
"""
df = series.to_frame(name=label) if label else series.to_frame()
df = df.transpose() if transpose else df
return (df,)
@@ -0,0 +1,39 @@
from typing import Any, Dict
import pandas as pd
class PandasTranspose:
"""
Transposes a DataFrame.
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", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe: pd.DataFrame) -> tuple:
"""
Transposes a DataFrame.
Args:
dataframe (DataFrame): The DataFrame.
Returns:
tuple: A tuple containing a JSON string of the transposed DataFrame.
"""
return (dataframe.transpose(),)