diff --git a/__init__.py b/__init__.py index 06b0791..a0e2d60 100644 --- a/__init__.py +++ b/__init__.py @@ -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", diff --git a/docs/reference/node_reference.md b/docs/reference/node_reference.md index 192dc65..16e06a1 100644 --- a/docs/reference/node_reference.md +++ b/docs/reference/node_reference.md @@ -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. | diff --git a/docs/reference/pandas_iloc_rows_slice_dataframe.md b/docs/reference/pandas_iloc_rows_slice_dataframe.md new file mode 100644 index 0000000..61ccdfe --- /dev/null +++ b/docs/reference/pandas_iloc_rows_slice_dataframe.md @@ -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 | + +
+Category: Data subset selection + +ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved. diff --git a/modules/pandas_wrapper/pandas_iloc_rows_slice_dataframe.py b/modules/pandas_wrapper/pandas_iloc_rows_slice_dataframe.py new file mode 100644 index 0000000..05ed476 --- /dev/null +++ b/modules/pandas_wrapper/pandas_iloc_rows_slice_dataframe.py @@ -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,) diff --git a/pyproject.toml b/pyproject.toml index 57fc9cd..810eb98 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.24" +version = "1.1.25" license = {file = "LICENSE"} [project.urls] diff --git a/tests/pandas_wrapper/pandas_iloc_rows_slice_dataframe_test.py b/tests/pandas_wrapper/pandas_iloc_rows_slice_dataframe_test.py new file mode 100644 index 0000000..f80d00b --- /dev/null +++ b/tests/pandas_wrapper/pandas_iloc_rows_slice_dataframe_test.py @@ -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() \ No newline at end of file