Added Pandas Iloc Rows Slice Dataframe node.

This commit is contained in:
HowToSD
2025-02-23 14:35:21 -08:00
parent 398a91f3d0
commit 41eb70b661
6 changed files with 101 additions and 1 deletions
+3
View File
@@ -43,6 +43,7 @@ from .modules.pandas_wrapper.pandas_horizontal_concat import PandasHorizontalCon
from .modules.pandas_wrapper.pandas_horizontal_split import PandasHorizontalSplit
from .modules.pandas_wrapper.pandas_iloc_row_series import PandasIlocRowSeries
from .modules.pandas_wrapper.pandas_iloc_rows_dataframe import PandasIlocRowsDataFrame
from .modules.pandas_wrapper.pandas_iloc_rows_slice_dataframe import PandasIlocRowsSliceDataFrame
from .modules.pandas_wrapper.pandas_index import PandasIndex
from .modules.pandas_wrapper.pandas_index_to_string import PandasIndexToString
from .modules.pandas_wrapper.pandas_isna import PandasIsNA
@@ -156,6 +157,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = {
"PandasHorizontalSplit": PandasHorizontalSplit,
"PandasIlocRowSeries": PandasIlocRowSeries,
"PandasIlocRowsDataFrame": PandasIlocRowsDataFrame,
"PandasIlocRowsSliceDataFrame": PandasIlocRowsSliceDataFrame,
"PandasIndex": PandasIndex,
"PandasIndexToString": PandasIndexToString,
"PandasIsNA": PandasIsNA,
@@ -269,6 +271,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = {
"PandasHorizontalSplit": "Pandas Horizontal Split",
"PandasIlocRowSeries": "Pandas Iloc Row Series",
"PandasIlocRowsDataFrame": "Pandas Iloc Rows DataFrame",
"PandasIlocRowsSliceDataFrame": "Pandas Iloc Rows Slice DataFrame",
"PandasIndex": "Pandas Index",
"PandasIndexToString": "Pandas Index To String",
"PandasIsNA": "Pandas Is NA",
+1
View File
@@ -48,6 +48,7 @@
| [Pandas Head](pandas_head.md) | Retrieves the first few rows of a pandas DataFrame. |
| [Pandas Iloc Row Series](pandas_iloc_row_series.md) | Selects a row from a pandas DataFrame and returning it as a Series. |
| [Pandas Iloc Rows Data Frame](pandas_iloc_rows_dataframe.md) | Selects rows from a pandas DataFrame and returning it as a DataFrame. |
| [Pandas Iloc Rows Slice Data Frame](pandas_iloc_rows_slice_dataframe.md) | Selects rows from a pandas DataFrame using a slice defined by a start and end integer position. The start position is inclusive, while the end position is exclusive. Returns the selected rows as a DataFrame. |
| [Pandas Loc Cell Str](pandas_loc_cell_str.md) | Selects a cell from a pandas DataFrame and output as a string. |
| [Pandas Loc Row MultiIndex DataFrame](pandas_loc_row_multiindex_dataframe.md) | Selects a row or rows from a pandas DataFrame with MultiIndex and output as a DataFrame. |
| [Pandas Loc Row Series](pandas_loc_row_series.md) | Selects a row from a pandas DataFrame and output as a Series. |
@@ -0,0 +1,19 @@
# Pandas Iloc Rows Slice Data Frame
Selects rows from a pandas DataFrame using a slice defined by a start and end integer position. The start position is inclusive, while the end position is exclusive. Returns the selected rows as a DataFrame.
## Input
| Name | Data type |
|---|---|
| dataframe | DataFrame |
| start_row | Int |
| end_row | Int |
## Output
| Data type |
|---|
| DataFrame |
<HR>
Category: Data subset selection
ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved.
@@ -0,0 +1,47 @@
from typing import Any, Dict
import pandas as pd
class PandasIlocRowsSliceDataFrame:
"""
Selects rows from a pandas DataFrame using a slice defined by a start and end integer position. The start position is inclusive, while the end position is exclusive. Returns the selected rows as a DataFrame.
category: Data subset selection
"""
@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", {}),
"start_row": ("INT", {"default": 0, "min": -1, "max": 2**31}),
"end_row": ("INT", {"default": 0, "min": -1, "max": 2**31})
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe: pd.DataFrame,
start_row: int,
end_row:int) -> tuple:
"""
Selects rows from a pandas DataFrame using a slice defined by a start and end integer position. The start position is inclusive, while the end position is exclusive. Returns the selected rows as a DataFrame.
Args:
dataframe (DataFrame): The DataFrame.
start_row (int): The integer position of the start row (inclusive).
end_row (int): The integer position of the end row (exclusive).
Returns:
tuple: A tuple containing the DataFrame.
"""
df2 = dataframe.iloc[start_row:end_row]
return (df2,)
+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "data-analysis"
description = "Data analysis custom modules for ComfyUI - Use Pandas & Matplotlib within ComfyUI"
version = "1.1.24"
version = "1.1.25"
license = {file = "LICENSE"}
[project.urls]
@@ -0,0 +1,30 @@
import os
import sys
import unittest
import pandas as pd
PROJECT_ROOT = os.path.realpath(os.path.join(__file__, "..", "..", ".."))
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
sys.path.append(MODULE_ROOT)
from pandas_wrapper.pandas_iloc_rows_slice_dataframe import PandasIlocRowsSliceDataFrame
class Test(unittest.TestCase):
def setUp(self):
"""Set up test data"""
self.node = PandasIlocRowsSliceDataFrame()
self.df = pd.DataFrame({
"purchase_price": [2, 3, 4, 6, 8, 5, 6],
"sales_price": [3, 6, 5, 7, 10, 4, 3],
"in_stock": ["Yes", "No", "Yes", "No", "Yes", "Yes", "Yes"]
}, index=["apple", "banana", "coconut", "donut", "egg", "fig", "guava"])
def test_iloc_rows_slice(self):
"""Test row slicing functionality"""
result_df, = self.node.f(self.df, 1, 4)
expected_df = self.df.iloc[1:4]
pd.testing.assert_frame_equal(result_df, expected_df)
if __name__ == "__main__":
unittest.main()