From a9736caea237ab817b5f2c5d206b32d8d3f295c3 Mon Sep 17 00:00:00 2001 From: HowToSD Date: Fri, 14 Feb 2025 15:01:14 -0800 Subject: [PATCH] Added Pandas Load CSV with Index node. --- README.md | 2 +- __init__.py | 3 ++ docs/ug.md | 3 +- examples/datasets/car_sales.csv | 4 ++ .../pandas_load_csv_with_index.py | 49 +++++++++++++++++++ pyproject.toml | 2 +- 6 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 examples/datasets/car_sales.csv create mode 100644 modules/pandas_wrapper/pandas_load_csv_with_index.py diff --git a/README.md b/README.md index 999aebc..6be248c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ComfyUI-Data-Analysis is a custom module for analyzing data using Pandas and Matplotlib within ComfyUI. -Nodes include: +Here are some of the included nodes: - Loading a CSV file as a DataFrame - Saving a DataFrame to a CSV file diff --git a/__init__.py b/__init__.py index 489bcfc..cbd7774 100644 --- a/__init__.py +++ b/__init__.py @@ -9,6 +9,7 @@ from .modules.pandas_wrapper.pandas_head import PandasHead from .modules.pandas_wrapper.pandas_index import PandasIndex from .modules.pandas_wrapper.pandas_join import PandasJoin from .modules.pandas_wrapper.pandas_load_csv import PandasLoadCSV +from .modules.pandas_wrapper.pandas_load_csv_with_index import PandasLoadCSVWithIndex from .modules.pandas_wrapper.pandas_save_csv import PandasSaveCSV from .modules.pandas_wrapper.pandas_select_columns import PandasSelectColumns from .modules.pandas_wrapper.pandas_select_rows import PandasSelectRows @@ -31,6 +32,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = { "PandasIndex": PandasIndex, "PandasJoin": PandasJoin, "PandasLoadCSV": PandasLoadCSV, + "PandasLoadCSVWithIndex": PandasLoadCSVWithIndex, "PandasSaveCSV": PandasSaveCSV, "PandasSelectColumns": PandasSelectColumns, "PandasSelectRows": PandasSelectRows, @@ -56,6 +58,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = { "PandasIndex": "Pandas Index", "PandasJoin": "Pandas Join", "PandasLoadCSV": "Pandas Load CSV", + "PandasLoadCSVWithIndex": "Pandas Load CSV with Index", "PandasSaveCSV": "Pandas Save CSV", "PandasSelectColumns": "Pandas Select Columns", "PandasSelectRows": "Pandas Select Rows", diff --git a/docs/ug.md b/docs/ug.md index 5f34f24..2a2efe7 100644 --- a/docs/ug.md +++ b/docs/ug.md @@ -14,6 +14,7 @@ You can right-click, select **Add Node**, go to **Data Analysis**, and look for | Node Name | Functionality | |-------------------------|-------------------------------------| | **Pandas Load CSV** | Load a CSV file | +| **Pandas Load CSV with Index** | Load a CSV file containing index | | **Pandas Save CSV** | Save a DataFrame to a CSV file | | **Pandas Create** | Create a DataFrame from CSV text field | | **Pandas Create with Index** | Create a DataFrame from CSV text field containing index | @@ -37,7 +38,7 @@ A faster way is to double-click the canvas to open the node search dialog. The examples directory contains workflows that load data from an example dataset included in this package. These workflows should give you a good idea about how to use these nodes. ## Loading data -Use **Pandas Load CSV** node to load a CSV. +Use **Pandas Load CSV** node to load a CSV, or **Pandas Load CSV with Index** node if the CSV contains an index column. ![Pandas Load CSV node](images/load.jpg) This will emit the DataFrame in a JSON string. diff --git a/examples/datasets/car_sales.csv b/examples/datasets/car_sales.csv new file mode 100644 index 0000000..b2cf61c --- /dev/null +++ b/examples/datasets/car_sales.csv @@ -0,0 +1,4 @@ +Index,Name,Sales +A,Foo,10000 +B,Bar,20000 +C,Baz,30000 diff --git a/modules/pandas_wrapper/pandas_load_csv_with_index.py b/modules/pandas_wrapper/pandas_load_csv_with_index.py new file mode 100644 index 0000000..0041280 --- /dev/null +++ b/modules/pandas_wrapper/pandas_load_csv_with_index.py @@ -0,0 +1,49 @@ +from typing import Any, Dict +from io import StringIO +import pandas as pd + + +class PandasLoadCSVWithIndex: + """ + PandasLoadCSV: + A class for loading CSV files into a pandas DataFrame. + Input data is assume to have an index. + """ + + @classmethod + def INPUT_TYPES(cls) -> Dict[str, Any]: + """ + Defines the input types for the `load_csv` function. + + Returns: + Dict[str, Any]: A dictionary specifying required input types. + """ + return { + "required": { + "file_path": ("STRING", {"multiline": False}), + "index_col": ("INT", {"default": 0, "min": 0, "max": 10000}) + } + } + + RETURN_TYPES: tuple = ("STRING",) + FUNCTION: str = "load_csv" + CATEGORY: str = "Data Analysis" + + def load_csv(self, file_path: str, index_col: int=0) -> tuple: + """ + Loads a CSV file into a pandas DataFrame and converts it to a JSON string. + Input data is assume to have an index. + + Args: + file_path (str): The path to the CSV file. + + Returns: + tuple: A tuple containing a JSON string representation of the DataFrame. + """ + # Read CSV file into DataFrame + df = pd.read_csv(file_path, index_col=index_col) + + # Convert DataFrame to JSON + result_json = df.to_json() + + return (result_json,) diff --git a/pyproject.toml b/pyproject.toml index e1fe96c..506316a 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.0.7" +version = "1.0.8" license = {file = "LICENSE"} [project.urls]