Added Pandas logical operations nodes.

This commit is contained in:
HowToSD
2025-03-07 12:03:58 -08:00
parent 4f0fc89004
commit f918096cb6
31 changed files with 1017 additions and 2 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ class CDAJSONCreate:
data (str): String value in the text field.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
tuple: A tuple containing a JSON string.
"""
# Convert to JSON
json_obj = json.loads(data)
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, Dict
import ast
class PyStringToDict:
"""
Creates a Python dictionary using the string values entered in the text field.
category: Python wrapper
"""
@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 = ("PYDICT",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, data: str) -> tuple:
"""
Creates a Python dictionary using the string values entered in the text field.
Args:
data (str): String value in the text field.
Returns:
tuple: A dict converted from the input string.
"""
d = ast.literal_eval(data)
return (d,)
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, List
import ast
class PyStringToList:
"""
Creates a Python list using the string values entered in the text field.
category: Python wrapper
"""
@classmethod
def INPUT_TYPES(cls) -> List[str]:
"""
Defines the input types for the `create` function.
Returns:
List[str, Any]: A list specifying required input types.
"""
return {
"required": {
"data": ("STRING", {"multiline": True}),
}
}
RETURN_TYPES: tuple = ("PYLIST",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, data: str) -> tuple:
"""
Creates a Python list using the string values entered in the text field.
Args:
data (str): String value in the text field.
Returns:
tuple: A list converted from the input string.
"""
d = ast.literal_eval(data)
return (d,)
@@ -12,6 +12,8 @@ class PandasCreateFromDict:
"""
Creates a pandas DataFrame from a Python dictionary.
Use `Py String To Dict` node to generate a dict from a string.
category: IO
"""
@@ -0,0 +1,59 @@
import os
import sys
from typing import Any, Dict, List
import pandas as pd
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
sys.path.append(MODULE_ROOT)
from cda.utils import python_dict_first_value_len
class PandasCreateFromDictIndexList:
"""
Creates a pandas DataFrame from a Python dictionary and a list for index.
Use `Py String To Dict` node to generate a dict for cell values from a string.
Use `Py String To List` node to generate a list for index from a string.
category: IO
"""
@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": ("PYDICT", {}),
"index": ("PYLIST", {}),
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, data: Dict, index: List) -> tuple:
"""
Creates a pandas DataFrame from a Python dictionary.
Args:
data (Dict): A Python dictionary containing cell values.
index (List): A Python list containing index.
Returns:
tuple: A tuple containing a the DataFrame.
"""
if not data:
df = pd.DataFrame() # Return an empty DataFrame if no data is provided.
else:
if not list:
value_length = python_dict_first_value_len(data)
df = pd.DataFrame(data, index=list(range(value_length)))
else:
df = pd.DataFrame(data, index=index)
return (df,)
+43
View File
@@ -0,0 +1,43 @@
from typing import Any, Dict
import pandas as pd
class PandasEq:
"""
Determines whether each element in the first pandas DataFrame is equal to the corresponding element in the second DataFrame.
category: Logical operations
"""
@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": {
"a_dataframe": ("DATAFRAME", {}),
"b_dataframe": ("DATAFRAME", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, a_dataframe: pd.DataFrame, b_dataframe: pd.DataFrame) -> tuple:
"""
Determines whether each element in the first pandas DataFrame is equal to the corresponding element in the second DataFrame.
Args:
a_dataframe (DataFrame): The left DataFrame.
b_dataframe (DataFrame): The right DataFrame.
Returns:
tuple: A tuple containing the DataFrame containing the result of operation.
"""
df_out = a_dataframe.eq(b_dataframe)
return (df_out,)
+43
View File
@@ -0,0 +1,43 @@
from typing import Any, Dict
import pandas as pd
class PandasGe:
"""
Determines whether each element in the first pandas DataFrame is greater than or equal to the corresponding element in the second DataFrame.
category: Logical operations
"""
@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": {
"a_dataframe": ("DATAFRAME", {}),
"b_dataframe": ("DATAFRAME", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, a_dataframe: pd.DataFrame, b_dataframe: pd.DataFrame) -> tuple:
"""
Determines whether each element in the first pandas DataFrame is greater than or equal to the corresponding element in the second DataFrame.
Args:
a_dataframe (DataFrame): The left DataFrame.
b_dataframe (DataFrame): The right DataFrame.
Returns:
tuple: A tuple containing the DataFrame containing the result of operation.
"""
df_out = a_dataframe.ge(b_dataframe)
return (df_out,)
+43
View File
@@ -0,0 +1,43 @@
from typing import Any, Dict
import pandas as pd
class PandasGt:
"""
Determines whether each element in the first pandas DataFrame is greater than the corresponding element in the second DataFrame.
category: Logical operations
"""
@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": {
"a_dataframe": ("DATAFRAME", {}),
"b_dataframe": ("DATAFRAME", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, a_dataframe: pd.DataFrame, b_dataframe: pd.DataFrame) -> tuple:
"""
Determines whether each element in the first pandas DataFrame is greater than the corresponding element in the second DataFrame.
Args:
a_dataframe (DataFrame): The left DataFrame.
b_dataframe (DataFrame): The right DataFrame.
Returns:
tuple: A tuple containing the DataFrame containing the result of operation.
"""
df_out = a_dataframe.gt(b_dataframe)
return (df_out,)
+43
View File
@@ -0,0 +1,43 @@
from typing import Any, Dict
import pandas as pd
class PandasLe:
"""
Determines whether each element in the first pandas DataFrame is less than or equal to the corresponding element in the second DataFrame.
category: Logical operations
"""
@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": {
"a_dataframe": ("DATAFRAME", {}),
"b_dataframe": ("DATAFRAME", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, a_dataframe: pd.DataFrame, b_dataframe: pd.DataFrame) -> tuple:
"""
Determines whether each element in the first pandas DataFrame is less than or equal to the corresponding element in the second DataFrame.
Args:
a_dataframe (DataFrame): The left DataFrame.
b_dataframe (DataFrame): The right DataFrame.
Returns:
tuple: A tuple containing the DataFrame containing the result of operation.
"""
df_out = a_dataframe.le(b_dataframe)
return (df_out,)
+43
View File
@@ -0,0 +1,43 @@
from typing import Any, Dict
import pandas as pd
class PandasLt:
"""
Determines whether each element in the first pandas DataFrame is less than the corresponding element in the second DataFrame.
category: Logical operations
"""
@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": {
"a_dataframe": ("DATAFRAME", {}),
"b_dataframe": ("DATAFRAME", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, a_dataframe: pd.DataFrame, b_dataframe: pd.DataFrame) -> tuple:
"""
Determines whether each element in the first pandas DataFrame is less than the corresponding element in the second DataFrame.
Args:
a_dataframe (DataFrame): The left DataFrame.
b_dataframe (DataFrame): The right DataFrame.
Returns:
tuple: A tuple containing the DataFrame containing the result of operation.
"""
df_out = a_dataframe.lt(b_dataframe)
return (df_out,)
+43
View File
@@ -0,0 +1,43 @@
from typing import Any, Dict
import pandas as pd
class PandasNe:
"""
Determines whether each element in the first pandas DataFrame is not equal to the corresponding element in the second DataFrame.
category: Logical operations
"""
@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": {
"a_dataframe": ("DATAFRAME", {}),
"b_dataframe": ("DATAFRAME", {})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, a_dataframe: pd.DataFrame, b_dataframe: pd.DataFrame) -> tuple:
"""
Determines whether each element in the first pandas DataFrame is not equal to the corresponding element in the second DataFrame.
Args:
a_dataframe (DataFrame): The left DataFrame.
b_dataframe (DataFrame): The right DataFrame.
Returns:
tuple: A tuple containing the DataFrame containing the result of operation.
"""
df_out = a_dataframe.ne(b_dataframe)
return (df_out,)