← All ivy-nodes
IVYXSTUDIO · IVY NODE
D
Data Frame To CSV
ivy.node.csv-from-data-frame · v0.1.0
ivyx✓
Writes a pandas DataFrame as CSV into an in-memory buffer and returns the buffer with its size. The buffer is a handle another node in the same run reads, such as csvto-string-converter or minio-file-upload.
#pandas#CSV#Data Conversion#File Handling#Data Export
Inputs
| Field | Type | Description |
|---|---|---|
| data_framerequired | Not declared | The data frame to write as CSV. |
Outputs
| Field | Type | Description |
|---|---|---|
| csv_bufferrequired | Not declared | The CSV bytes, positioned at the start. |
| size_bytesrequired | integer | How many bytes of CSV were written. |
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_buffer
out["size_bytes"] = len(csv_buffer.getvalue())Tests
Requires: python:3.9
- two-rows
A two-row frame writes a header and two lines, 12 bytes in all.
- not-a-frame
Something that is not a DataFrame is refused.