|
5 | 5 | import pyarrow as pa # type: ignore |
6 | 6 | except Exception: |
7 | 7 | pa = None |
| 8 | +try: |
| 9 | + import pandas as pd # type: ignore |
| 10 | +except Exception: |
| 11 | + pd = None |
8 | 12 | from nisystemlink.clients.core import HttpConfiguration |
9 | 13 | from nisystemlink.clients.dataframe import DataFrameClient |
10 | 14 | from nisystemlink.clients.dataframe.models import ( |
|
40 | 44 | ) |
41 | 45 | ) |
42 | 46 |
|
| 47 | + |
| 48 | +print(f"Created table with ID: {table_id}") |
| 49 | + |
| 50 | +print("Appending data to table...") |
| 51 | + |
43 | 52 | # Append via explicit AppendTableDataRequest (JSON) |
44 | 53 | frame_request = DataFrame( |
45 | 54 | data=[[str(i), str(random.random()), datetime.now().isoformat()] for i in range(3)] |
|
55 | 64 | client.append_table_data(table_id, frame_direct) |
56 | 65 |
|
57 | 66 | if pa is not None: |
| 67 | + print("Appending data to table via Arrow RecordBatches...") |
58 | 68 | # Append via single RecordBatch (Arrow) |
59 | 69 | batch_single = pa.record_batch( |
60 | 70 | [ |
|
79 | 89 | ] |
80 | 90 | client.append_table_data(table_id, batch_list) |
81 | 91 |
|
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