-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add DataToDataFrame component for converting Data objects #6112
Open
Cristhianzl
wants to merge
9
commits into
main
Choose a base branch
from
cz/data-to-dataframe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dd89317
✨ (data_to_dataframe.py): add a new component to convert Data objects…
Cristhianzl 48d92fa
[autofix.ci] apply automated fixes
autofix-ci[bot] 91cb01e
📝 (data_to_dataframe.py): improve documentation for the build_datafra…
Cristhianzl c2a1cb7
📝 (data_to_dataframe.py): add missing comma in the component definiti…
Cristhianzl 29d7c5a
✨ (test_data_to_dataframe.py): Add unit tests for DataToDataFrameComp…
Cristhianzl 7d77989
✨ (test_data_to_dataframe.py): Refactor test_data_to_dataframe.py to …
Cristhianzl da81527
[autofix.ci] apply automated fixes
autofix-ci[bot] de0c75a
🔧 (test_data_to_dataframe.py): improve variable naming for better rea…
Cristhianzl ed67906
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/backend/base/langflow/components/processing/data_to_dataframe.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from langflow.custom import Component | ||
from langflow.io import DataInput, Output | ||
from langflow.schema import Data, DataFrame | ||
|
||
|
||
class DataToDataFrameComponent(Component): | ||
display_name = "Data → DataFrame" | ||
description = ( | ||
"Converts one or multiple Data objects into a DataFrame. " | ||
"Each Data object corresponds to one row. Fields from `.data` become columns, " | ||
"and the `.text` (if present) is placed in a 'text' column." | ||
) | ||
icon = "table" | ||
name = "DataToDataFrame" | ||
|
||
inputs = [ | ||
DataInput( | ||
name="data_list", | ||
display_name="Data or Data List", | ||
info="One or multiple Data objects to transform into a DataFrame.", | ||
is_list=True, | ||
), | ||
] | ||
|
||
outputs = [ | ||
Output( | ||
display_name="DataFrame", | ||
name="dataframe", | ||
method="build_dataframe", | ||
info="A DataFrame built from each Data object's fields plus a 'text' column.", | ||
), | ||
] | ||
|
||
def build_dataframe(self) -> DataFrame: | ||
"""Builds a DataFrame from Data objects by combining their fields. | ||
|
||
For each Data object: | ||
- Merge item.data (dictionary) as columns | ||
- If item.text is present, add 'text' column | ||
|
||
Returns a DataFrame with one row per Data object. | ||
""" | ||
data_input = self.data_list | ||
|
||
# If user passed a single Data, it might come in as a single object rather than a list | ||
if not isinstance(data_input, list): | ||
data_input = [data_input] | ||
|
||
rows = [] | ||
for item in data_input: | ||
if not isinstance(item, Data): | ||
msg = f"Expected Data objects, got {type(item)} instead." | ||
raise TypeError(msg) | ||
|
||
# Start with a copy of item.data or an empty dict | ||
row_dict = dict(item.data) if item.data else {} | ||
|
||
# If the Data object has text, store it under 'text' col | ||
text_val = item.get_text() | ||
if text_val: | ||
row_dict["text"] = text_val | ||
|
||
rows.append(row_dict) | ||
|
||
# Build a DataFrame from these row dictionaries | ||
df_result = DataFrame(rows) | ||
self.status = df_result # store in self.status for logs | ||
return df_result |
127 changes: 127 additions & 0 deletions
127
src/backend/tests/unit/components/processing/test_data_to_dataframe.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import pytest | ||
from langflow.components.processing.data_to_dataframe import DataToDataFrameComponent | ||
from langflow.schema import Data, DataFrame | ||
|
||
from tests.base import ComponentTestBaseWithoutClient | ||
|
||
|
||
class TestDataToDataFrameComponent(ComponentTestBaseWithoutClient): | ||
@pytest.fixture | ||
def component_class(self): | ||
"""Return the component class to test.""" | ||
return DataToDataFrameComponent | ||
|
||
@pytest.fixture | ||
def default_kwargs(self): | ||
"""Return the default kwargs for the component.""" | ||
return { | ||
"data_list": [ | ||
Data(text="Row 1", data={"field1": "value1", "field2": 1}), | ||
Data(text="Row 2", data={"field1": "value2", "field2": 2}), | ||
] | ||
} | ||
|
||
@pytest.fixture | ||
def file_names_mapping(self): | ||
"""Return the file names mapping for different versions.""" | ||
# This is a new component, so we return an empty list | ||
return [] | ||
|
||
def test_basic_setup(self, component_class, default_kwargs): | ||
"""Test basic component initialization.""" | ||
component = component_class() | ||
component.set_attributes(default_kwargs) | ||
assert component.data_list == default_kwargs["data_list"] | ||
|
||
def test_build_dataframe_basic(self, component_class, default_kwargs): | ||
"""Test basic DataFrame construction.""" | ||
component = component_class() | ||
component.set_attributes(default_kwargs) | ||
result_df = component.build_dataframe() | ||
|
||
assert isinstance(result_df, DataFrame) | ||
assert len(result_df) == 2 | ||
assert list(result_df.columns) == ["field1", "field2", "text"] | ||
assert result_df["text"].tolist() == ["Row 1", "Row 2"] | ||
assert result_df["field1"].tolist() == ["value1", "value2"] | ||
assert result_df["field2"].tolist() == [1, 2] | ||
|
||
def test_single_data_input(self, component_class): | ||
"""Test handling single Data object input.""" | ||
single_data = Data(text="Single Row", data={"field1": "value"}) | ||
component = component_class() | ||
component.set_attributes({"data_list": single_data}) | ||
|
||
result_df = component.build_dataframe() | ||
|
||
assert len(result_df) == 1 | ||
assert result_df["text"].iloc[0] == "Single Row" | ||
assert result_df["field1"].iloc[0] == "value" | ||
|
||
def test_empty_data_list(self, component_class): | ||
"""Test behavior with empty data list.""" | ||
component = component_class() | ||
component.set_attributes({"data_list": []}) | ||
|
||
result_df = component.build_dataframe() | ||
|
||
assert len(result_df) == 0 | ||
|
||
def test_data_without_text(self, component_class): | ||
"""Test handling Data objects without text field.""" | ||
data_without_text = [Data(data={"field1": "value1"}), Data(data={"field1": "value2"})] | ||
component = component_class() | ||
component.set_attributes({"data_list": data_without_text}) | ||
|
||
result_df = component.build_dataframe() | ||
|
||
assert len(result_df) == 2 | ||
assert "text" not in result_df.columns | ||
assert result_df["field1"].tolist() == ["value1", "value2"] | ||
|
||
def test_data_without_data_dict(self, component_class): | ||
"""Test handling Data objects without data dictionary.""" | ||
data_without_dict = [Data(text="Text 1"), Data(text="Text 2")] | ||
component = component_class() | ||
component.set_attributes({"data_list": data_without_dict}) | ||
|
||
result_df = component.build_dataframe() | ||
|
||
assert len(result_df) == 2 | ||
assert list(result_df.columns) == ["text"] | ||
assert result_df["text"].tolist() == ["Text 1", "Text 2"] | ||
|
||
def test_mixed_data_fields(self, component_class): | ||
"""Test handling Data objects with different fields.""" | ||
mixed_data = [ | ||
Data(text="Row 1", data={"field1": "value1", "field2": 1}), | ||
Data(text="Row 2", data={"field1": "value2", "field3": "extra"}), | ||
] | ||
component = component_class() | ||
component.set_attributes({"data_list": mixed_data}) | ||
|
||
result_df = component.build_dataframe() | ||
|
||
assert len(result_df) == 2 | ||
assert set(result_df.columns) == {"field1", "field2", "field3", "text"} | ||
assert result_df["field1"].tolist() == ["value1", "value2"] | ||
assert result_df["field2"].iloc[1] != result_df["field2"].iloc[1] # Check for NaN using inequality | ||
assert result_df["field3"].iloc[0] != result_df["field3"].iloc[0] # Check for NaN using inequality | ||
|
||
def test_invalid_input_type(self, component_class): | ||
"""Test error handling for invalid input types.""" | ||
invalid_data = [{"not": "a Data object"}] | ||
component = component_class() | ||
component.set_attributes({"data_list": invalid_data}) | ||
|
||
with pytest.raises(TypeError) as exc_info: | ||
component.build_dataframe() | ||
assert "Expected Data objects" in str(exc_info.value) | ||
|
||
def test_status_update(self, component_class, default_kwargs): | ||
"""Test that status is properly updated.""" | ||
component = component_class() | ||
component.set_attributes(default_kwargs) | ||
result = component.build_dataframe() | ||
|
||
assert component.status is result # Status should be set to the DataFrame |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.