← All ivy-nodes
IVYXSTUDIO · IVY NODE
D
Data Frame To CSV
ivy.node.csv-from-data-frame · v0.0.0
ivyx✓
Converts a pandas DataFrame into CSV format stored in a BytesIO buffer. Use this node to export data from data processing pipelines. Typically used after data-frame-creator or data transformation nodes. The output buffer can be used with email-builder for attachments, minio-file-upload for storage, or csvto-string-converter for string representation.
#pandas#CSV#Data Conversion#File Handling#Data Export
Inputs
| Field | Type | Description |
|---|---|---|
| data_framerequired | object | A pandas DataFrame to be converted to CSV. (Node reference) |
| features | object | Features |
| labels | object | Labels |
Outputs
| Field | Type | Description |
|---|---|---|
| csv_bufferrequired | object | Custom type: io.BytesIO |
| features | object | Features |
| labels | object | Labels |
Source
python
# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
data_frame = inp["data_frame"]
# Compute
import pandas as pd
import io
class DataFrameToCSVBuffer:
"""
A class to convert a pandas DataFrame into a CSV format and store it in a BytesIO buffer.
"""
def __init__(self, data_frame: pd.DataFrame):
"""
Initialize the DataFrameToCSVBuffer with a pandas DataFrame.
:param data_frame: A pandas DataFrame to be converted to CSV.
"""
self.data_frame = data_frame
def to_buffer(self) -> io.BytesIO:
"""
Convert the DataFrame to CSV format and write it to a BytesIO buffer.
:return: A BytesIO object containing the CSV data.
"""
buffer = io.BytesIO()
self.data_frame.to_csv(buffer, index=False)
buffer.seek(0) # Reset the buffer's position to the beginning
return buffer
data_frame_to_csv_buffer = DataFrameToCSVBuffer(data_frame)
csv_buffer = data_frame_to_csv_buffer.to_buffer()
# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["csv_buffer"] = csv_bufferTests
Requires: python:3.11
- basic-conversion
Convert valid DataFrame to CSV buffer (should succeed).
csvdataframeconversionbasic - empty-dataframe
Convert empty DataFrame (should succeed with empty CSV).
csvdataframeempty