-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_dataframe.py
More file actions
320 lines (283 loc) · 13.8 KB
/
test_dataframe.py
File metadata and controls
320 lines (283 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import os
from datetime import date
import numpy as np
import pandas as pd
import pytest
from tsfile import ColumnSchema, TableSchema, TSDataType
from tsfile import TsFileTableWriter, ColumnCategory
from tsfile import to_dataframe
from tsfile.exceptions import ColumnNotExistError, TypeMismatchError
def convert_to_nullable_types(df):
"""
Convert DataFrame columns to nullable types to match returned DataFrame from to_dataframe.
This handles the fact that returned DataFrames use nullable types (Int64, Float64, etc.)
to support Null values.
"""
df = df.copy()
for col in df.columns:
dtype = df[col].dtype
if dtype == 'int64':
df[col] = df[col].astype('Int64')
elif dtype == 'int32':
df[col] = df[col].astype('Int32')
elif dtype == 'float64':
df[col] = df[col].astype('Float64')
elif dtype == 'float32':
df[col] = df[col].astype('Float32')
elif dtype == 'bool':
df[col] = df[col].astype('boolean')
return df
def test_write_dataframe_basic():
table = TableSchema("test_table",
[ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("value2", TSDataType.INT64, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_basic.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'time': [i for i in range(100)],
'device': [f"device{i}" for i in range(100)],
'value': [i * 1.5 for i in range(100)],
'value2': [i * 10 for i in range(100)]
})
writer.write_dataframe(df)
df_read = to_dataframe(tsfile_path, table_name="test_table")
df_read = df_read.sort_values('time').reset_index(drop=True)
df_sorted = convert_to_nullable_types(df.sort_values('time').reset_index(drop=True))
assert df_read.shape == (100, 4)
assert df_read["time"].equals(df_sorted["time"])
assert df_read["device"].equals(df_sorted["device"])
assert df_read["value"].equals(df_sorted["value"])
assert df_read["value2"].equals(df_sorted["value2"])
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_with_index():
table = TableSchema("test_table",
[ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_index.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'device': [f"device{i}" for i in range(50)],
'value': [i * 2.5 for i in range(50)]
})
df.index = [i * 10 for i in range(50)] # Set index as timestamps
writer.write_dataframe(df)
df_read = to_dataframe(tsfile_path, table_name="test_table")
df_read = df_read.sort_values('time').reset_index(drop=True)
df_sorted = df.sort_index()
df_sorted = convert_to_nullable_types(df_sorted.reset_index(drop=True))
time_series = pd.Series(df.sort_index().index.values, dtype='Int64')
assert df_read.shape == (50, 3)
assert df_read["time"].equals(time_series)
assert df_read["device"].equals(df_sorted["device"])
assert df_read["value"].equals(df_sorted["value"])
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_case_insensitive():
table = TableSchema("test_table",
[ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_case.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'Time': [i for i in range(30)], # Capital T
'Device': [f"device{i}" for i in range(30)], # Capital D
'VALUE': [i * 3.0 for i in range(30)] # All caps
})
writer.write_dataframe(df)
df_read = to_dataframe(tsfile_path, table_name="test_table")
df_read = df_read.sort_values('time').reset_index(drop=True)
df_sorted = convert_to_nullable_types(df.sort_values('Time').reset_index(drop=True))
assert df_read.shape == (30, 3)
assert df_read["time"].equals(df_sorted["Time"])
assert df_read["device"].equals(df_sorted["Device"])
assert df_read["value"].equals(df_sorted["VALUE"])
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_column_not_in_schema():
table = TableSchema("test_table",
[ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_extra_col.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'time': [i for i in range(10)],
'device': [f"device{i}" for i in range(10)],
'value': [i * 1.0 for i in range(10)],
'extra_column': [i for i in range(10)] # Not in schema
})
with pytest.raises(ColumnNotExistError):
writer.write_dataframe(df)
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_type_mismatch():
table = TableSchema("test_table",
[ColumnSchema("value", TSDataType.STRING, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_type_mismatch.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'time': [i for i in range(10)],
'value': [i for i in range(10)]
})
with pytest.raises(TypeMismatchError) as exc_info:
writer.write_dataframe(df)
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_all_datatypes():
table = TableSchema("test_table",
[ColumnSchema("bool_col", TSDataType.BOOLEAN, ColumnCategory.FIELD),
ColumnSchema("int32_col", TSDataType.INT32, ColumnCategory.FIELD),
ColumnSchema("int64_col", TSDataType.INT64, ColumnCategory.FIELD),
ColumnSchema("float_col", TSDataType.FLOAT, ColumnCategory.FIELD),
ColumnSchema("double_col", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("string_col", TSDataType.STRING, ColumnCategory.FIELD),
ColumnSchema("blob_col", TSDataType.BLOB, ColumnCategory.FIELD),
ColumnSchema("text_col", TSDataType.TEXT, ColumnCategory.FIELD),
ColumnSchema("date_col", TSDataType.DATE, ColumnCategory.FIELD),
ColumnSchema("timestamp_col", TSDataType.TIMESTAMP, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_all_types.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'time': [i for i in range(50)],
'bool_col': [i % 2 == 0 for i in range(50)],
'int32_col': pd.Series([i for i in range(50)], dtype='int32'),
'int64_col': [i * 10 for i in range(50)],
'float_col': pd.Series([i * 1.5 for i in range(50)], dtype='float32'),
'double_col': [i * 2.5 for i in range(50)],
'string_col': [f"str{i}" for i in range(50)],
'blob_col': [f"blob{i}".encode('utf-8') for i in range(50)],
'text_col': [f"text{i}" for i in range(50)],
'date_col': [date(2025, i % 11 + 1, i % 20 + 1) for i in range(50)],
'timestamp_col': [i for i in range(50)]
})
writer.write_dataframe(df)
df_read = to_dataframe(tsfile_path, table_name="test_table")
df_read = df_read.sort_values('time').reset_index(drop=True)
df_sorted = convert_to_nullable_types(df.sort_values('time').reset_index(drop=True))
assert df_read.shape == (50, 11)
assert df_read["bool_col"].equals(df_sorted["bool_col"])
assert df_read["int32_col"].equals(df_sorted["int32_col"])
assert df_read["int64_col"].equals(df_sorted["int64_col"])
assert np.allclose(df_read["float_col"], df_sorted["float_col"])
assert np.allclose(df_read["double_col"], df_sorted["double_col"])
assert df_read["string_col"].equals(df_sorted["string_col"])
assert df_read["blob_col"].equals(df_sorted["blob_col"])
assert df_read["text_col"].equals(df_sorted["text_col"])
assert df_read["date_col"].equals(df_sorted["date_col"])
assert df_read["timestamp_col"].equals(df_sorted["timestamp_col"])
for i in range(50):
assert df_read["blob_col"].iloc[i] == df_sorted["blob_col"].iloc[i]
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_schema_time_column():
table = TableSchema("test_table",
[ColumnSchema("time", TSDataType.TIMESTAMP, ColumnCategory.TIME),
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_schema_time.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'time': [i * 100 for i in range(50)],
'device': [f"device{i}" for i in range(50)],
'value': [i * 1.5 for i in range(50)]
})
writer.write_dataframe(df)
df_read = to_dataframe(tsfile_path, table_name="test_table")
df_read = df_read.sort_values('time').reset_index(drop=True)
df_sorted = convert_to_nullable_types(df.sort_values('time').reset_index(drop=True))
assert df_read.shape == (50, 3)
assert df_read["time"].equals(df_sorted["time"])
assert df_read["device"].equals(df_sorted["device"])
assert df_read["value"].equals(df_sorted["value"])
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_schema_time_and_dataframe_time():
table = TableSchema("test_table",
[ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_schema_and_df_time.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'Time': [i for i in range(30)],
'device': [f"dev{i}" for i in range(30)],
'value': [float(i) for i in range(30)]
})
writer.write_dataframe(df)
df_read = to_dataframe(tsfile_path, table_name="test_table")
df_read = df_read.sort_values('time').reset_index(drop=True)
df_sorted = convert_to_nullable_types(
df.sort_values('Time').rename(columns=str.lower).reset_index(drop=True)
)
assert df_read.shape == (30, 3)
assert df_read["time"].equals(df_sorted["time"])
assert df_read["device"].equals(df_sorted["device"])
assert df_read["value"].equals(df_sorted["value"])
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
def test_write_dataframe_empty():
table = TableSchema("test_table",
[ColumnSchema("value", TSDataType.DOUBLE, ColumnCategory.FIELD)])
tsfile_path = "test_write_dataframe_empty.tsfile"
try:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)
with TsFileTableWriter(tsfile_path, table) as writer:
df = pd.DataFrame({
'time': [],
'value': []
})
with pytest.raises(ValueError) as err:
writer.write_dataframe(df)
finally:
if os.path.exists(tsfile_path):
os.remove(tsfile_path)