jt2594838 commented on code in PR #706: URL: https://github.com/apache/tsfile/pull/706#discussion_r2780219265
########## python/tests/test_to_tsfile.py: ########## @@ -0,0 +1,378 @@ +# 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 to_dataframe, TsFileReader, ColumnCategory +from tsfile.utils import dataframe_to_tsfile + + +def convert_to_nullable_types(df): + 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_dataframe_to_tsfile_basic(): + tsfile_path = "test_dataframe_to_tsfile_basic.tsfile" + try: + if os.path.exists(tsfile_path): + os.remove(tsfile_path) + + 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)] + }) + + dataframe_to_tsfile(df, tsfile_path, table_name="test_table") + + 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_dataframe_to_tsfile_default_table_name(): + tsfile_path = "test_dataframe_to_tsfile_default.tsfile" + try: + if os.path.exists(tsfile_path): + os.remove(tsfile_path) + + df = pd.DataFrame({'time': [0, 1], 'value': [1.0, 2.0]}) + dataframe_to_tsfile(df, tsfile_path) + + df_read = to_dataframe(tsfile_path, table_name="default_table") + assert len(df_read) == 2 + finally: + if os.path.exists(tsfile_path): + os.remove(tsfile_path) + + +def test_dataframe_to_tsfile_with_index(): + tsfile_path = "test_dataframe_to_tsfile_index.tsfile" + try: + if os.path.exists(tsfile_path): + os.remove(tsfile_path) + + df = pd.DataFrame({ + 'device': [f"device{i}" for i in range(30)], + 'value': [i * 2.0 for i in range(30)] + }) + df.index = [i * 100 for i in range(30)] + dataframe_to_tsfile(df, tsfile_path, table_name="test_table") + + df_read = to_dataframe(tsfile_path, table_name="test_table") + df_read = df_read.sort_values('time').reset_index(drop=True) + time_expected = pd.Series(df.index.values, dtype='Int64') + assert df_read.shape == (30, 3) + assert df_read["time"].equals(time_expected) + + with TsFileReader(tsfile_path) as reader: + table_schema = reader.get_table_schema("test_table") + device_col = table_schema.get_column("device") + assert device_col is not None + assert device_col.get_category() == ColumnCategory.FIELD + finally: + if os.path.exists(tsfile_path): + os.remove(tsfile_path) + + +def test_dataframe_to_tsfile_custom_time_column(): + tsfile_path = "test_dataframe_to_tsfile_custom_time.tsfile" + try: + if os.path.exists(tsfile_path): + os.remove(tsfile_path) + + df = pd.DataFrame({ + 'timestamp': [i for i in range(30)], + 'device': [f"device{i}" for i in range(30)], + 'value': [i * 3.0 for i in range(30)] + }) + + dataframe_to_tsfile(df, tsfile_path, table_name="test_table", time_column="timestamp") + + 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('timestamp').reset_index(drop=True)) + + assert df_read.shape == (30, 3) + assert df_read["time"].equals(df_sorted["timestamp"]) + assert df_read["device"].equals(df_sorted["device"]) + assert df_read["value"].equals(df_sorted["value"]) Review Comment: The name of the time column should be prevserved. ########## cpp/src/cwrapper/tsfile_cwrapper.cc: ########## @@ -687,6 +691,9 @@ ERRNO _tsfile_writer_register_table(TsFileWriter writer, TableSchema* schema) { measurement_schemas.resize(schema->column_num); for (int i = 0; i < schema->column_num; i++) { ColumnSchema* cur_schema = schema->column_schemas + i; + if (cur_schema->column_category == TIME) { + continue; + } Review Comment: If you skip the time column, it will be missing when the file is loaded into IoTDB. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
