Skip to content

Commit 9e63a18

Browse files
feat: add example to append pandas df to DFS
1 parent 26a2e86 commit 9e63a18

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

examples/dataframe/create_write_data.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import pyarrow as pa # type: ignore
66
except Exception:
77
pa = None
8+
try:
9+
import pandas as pd # type: ignore
10+
except Exception:
11+
pd = None
812
from nisystemlink.clients.core import HttpConfiguration
913
from nisystemlink.clients.dataframe import DataFrameClient
1014
from nisystemlink.clients.dataframe.models import (
@@ -40,6 +44,11 @@
4044
)
4145
)
4246

47+
48+
print(f"Created table with ID: {table_id}")
49+
50+
print("Appending data to table...")
51+
4352
# Append via explicit AppendTableDataRequest (JSON)
4453
frame_request = DataFrame(
4554
data=[[str(i), str(random.random()), datetime.now().isoformat()] for i in range(3)]
@@ -55,6 +64,7 @@
5564
client.append_table_data(table_id, frame_direct)
5665

5766
if pa is not None:
67+
print("Appending data to table via Arrow RecordBatches...")
5868
# Append via single RecordBatch (Arrow)
5969
batch_single = pa.record_batch(
6070
[
@@ -79,9 +89,32 @@
7989
]
8090
client.append_table_data(table_id, batch_list)
8191

82-
# Mark end_of_data for the table
83-
# Supply `None` and `end_of_data=True`
84-
client.append_table_data(table_id, None, end_of_data=True)
85-
else:
86-
# If pyarrow not installed, flush via JSON path
87-
client.append_table_data(table_id, None, end_of_data=True)
92+
if pd is not None:
93+
print("Appending data to table via Pandas DataFrame...")
94+
# Append via DataFrame (Pandas)
95+
df = pd.DataFrame(
96+
{
97+
"ix": [11, 12, 13],
98+
"Float_Column": [0.6, 0.7, 0.8],
99+
"Timestamp_Column": [datetime.now() for _ in range(3)],
100+
}
101+
)
102+
103+
# Optional - coerce df types to the dataframe table schema
104+
df = df.astype(
105+
{
106+
"ix": "Int32",
107+
"Float_Column": "float32",
108+
"Timestamp_Column": "datetime64[ns]",
109+
}
110+
)
111+
112+
# convert Pandas DataFrame to Arrow RecordBatch
113+
batch_single = pa.record_batch(df)
114+
115+
client.append_table_data(table_id, batch_single)
116+
117+
# Mark end_of_data for the table
118+
# Supply `None` and `end_of_data=True`
119+
print("Finished appending data.")
120+
client.append_table_data(table_id, None, end_of_data=True)

0 commit comments

Comments
 (0)