forked from apache/tsfile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TsFile Python. * Fix macos and windows build * fix import error in windows python over 3.8. * refine python code. --------- Co-authored-by: Haonan <hhaonan@outlook.com>
- Loading branch information
Showing
15 changed files
with
1,279 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<!-- | ||
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. | ||
--> | ||
|
||
# TsFile Python Document | ||
|
||
<pre> | ||
___________ ___________.__.__ | ||
\__ ___/____\_ _____/|__| | ____ | ||
| | / ___/| __) | | | _/ __ \ | ||
| | \___ \ | \ | | |_\ ___/ | ||
|____|/____ >\___ / |__|____/\___ > version 1.0.0 | ||
\/ \/ \/ | ||
</pre> | ||
|
||
|
||
## Introduction | ||
|
||
This directory contains the Python implementation of TsFile. The Python version is built on the CPP version and uses the Cython package to integrate TsFile's read and write capabilities into the Python environment. Users can read and write TsFile as easily as they use read_csv and write_csv in Pandas. | ||
|
||
The source code can be found in the `./tsfile` directory. Files ending with `.pyx` and `.pyd` are wrapper code written in Cython. The `tsfile/tsfile.py` defines some user interfaces. You can find some examples of reading and writing in the `.examples/examples.py`. | ||
|
||
|
||
## How to make contributions | ||
|
||
Using pylint to check Python code is recommended. However, there is no suitable style checking tool for Cython code, and this part of the code should be consistent with the Python style required by pylint. | ||
|
||
**Feature List** | ||
- [ ] In pywrapper, invoke the batch reading interface implemented in CPP version of TsFile. | ||
- [ ] Supports writing multiple DataFrames into one single TsFile. | ||
|
||
|
||
|
||
## Build | ||
|
||
Before constructing Python version of TsFile, it is necessary to build [CPP version of TsFile](../cpp/README.md) first, because Python version of TsFile relies on the shared library files provided by CPP version of TsFile. | ||
|
||
Build by mvn in root directory: | ||
|
||
```sh | ||
mvn -P with-cpp,with-python clean verify | ||
``` | ||
|
||
Build by python command: | ||
|
||
```sh | ||
python setup.py build_ext --inplace | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# 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 numpy as np | ||
import pandas as pd | ||
import os | ||
|
||
import tsfile as ts | ||
|
||
|
||
# test writing data | ||
data_dir = os.path.join(os.path.dirname(__file__), "test.tsfile") | ||
DEVICE_NAME = "test_table" | ||
|
||
# 1000 rows data | ||
time = np.arange(1, 1001, dtype=np.int64) | ||
level = np.linspace(2000, 3000, num=1000, dtype=np.float32) | ||
num = np.arange(10000, 11000, dtype=np.int64) | ||
df = pd.DataFrame({"Time": time, "level": level, "num": num}) | ||
|
||
if os.path.exists(data_dir): | ||
os.remove(data_dir) | ||
ts.write_tsfile(data_dir, DEVICE_NAME, df) | ||
|
||
|
||
# read data we already wrote | ||
# with 20 chunksize | ||
tsfile_ret = ts.read_tsfile(data_dir, DEVICE_NAME, ["level", "num"], chunksize=20) | ||
print(tsfile_ret.shape) | ||
|
||
# with 100 chunksize | ||
tsfile_ret = ts.read_tsfile(data_dir, DEVICE_NAME, ["level", "num"], chunksize=100) | ||
print(tsfile_ret.shape) | ||
|
||
# get all data | ||
tsfile_ret = ts.read_tsfile(data_dir, DEVICE_NAME, ["level", "num"]) | ||
print(tsfile_ret.shape) | ||
|
||
# with iterator | ||
with ts.read_tsfile( | ||
data_dir, DEVICE_NAME, ["level", "num"], iterator=True, chunksize=100 | ||
) as reader: | ||
for chunk in reader: | ||
print(chunk.shape) | ||
|
||
# with time scale and chunksize | ||
tsfile_ret = ts.read_tsfile( | ||
data_dir, DEVICE_NAME, ["level"], start_time=50, end_time=100, chunksize=10 | ||
) | ||
print(tsfile_ret.shape) | ||
|
||
# with time scale | ||
tsfile_ret = ts.read_tsfile(data_dir, DEVICE_NAME, ["num"], start_time=50, end_time=100) | ||
print(tsfile_ret.shape) | ||
|
||
# with time scale, iterator and chunksize | ||
with ts.read_tsfile( | ||
data_dir, | ||
DEVICE_NAME, | ||
["level", "num"], | ||
iterator=True, | ||
start_time=100, | ||
end_time=500, | ||
chunksize=100, | ||
) as reader: | ||
for chunk in reader: | ||
print(chunk.shape) |
Oops, something went wrong.