k2pipe
Project description
Pipe SDK (暂用名)
通过配置的方式,简化数据处理流程中常用操作,使得数据处理的业务逻辑更加直观和稳定。
一、安装
要求python 3.8或以上,建议3.8.10版本。
pip install -U k2pipe
二、使用方法
2.1 数据加工
提供 extract_features() 方法,接受原始DataFrame和配置DataFrame两个参数,输出处理后的DataFrame。
2.1.1 配置文件
配置DataFrame一般从csv文件中加载,它定义了对原始DataFrame进行数据转换的规则。配置文件中每一行定义结果DataFrame里的一个(新)列,其中feature是列名,expression是列的计算表达式。
配置里定义的列是顺序处理的,因此列表达式既可以引用原始DataFrame里的列,也可以引用前面新定义的列。
| feature | expression | comment |
|---|---|---|
| feat1 | col1 / 100 - col2 | 四则运算示例 |
| feat2 | col2.rolling(3).mean() | 滚动窗口示例 |
| feat3 | col2.rolling('3D').mean() | 按时间滚动窗口示例(dataframe必须有时间索引列) |
| feat4 | feat1.my_func() | 自定义函数示例(函数需要在python代码里注册) |
| feat5 | "where(col1 > 50, col1, col2 * 2)" | 条件赋值示例(支持多层where嵌套) |
| # | 注释内容 | 注释行示例 |
| feat6 | (col3 - k_ts).dt.days | 时间处理示例 |
| * | 表示保留原始dataframe的所有列 | |
| feat7 | k_device.str[1] | 字符串操作示例 |
| feat8 | "where(col1.isna(), 1, 2)" | 空值判断示例 |
| feat9 | @df.shape[0] | 获取待处理的DataFrame的属性示例 |
说明:
- 若表达式或注释包含逗号,需要用双引号包裹,双引号前不能有空格
- 表达式不支持apply()函数
- 内置变量列表:@df
- 内置函数列表:暂无
2.2.2 Python代码
根据配置文件里定义的处理规则,将原始DataFrame数据转换为结果DataFrame数据。
import pandas as pd
from pathlib import Path
from k2pipe.pipe import init_pipe
# 样例数据
df = pd.DataFrame({'k_device': ['dev1','dev1', 'dev1', 'dev2', 'dev2', 'dev2'],
'col1': [50, 60, 70, 80, 90, 100],
'col2': [1, 2, 3, 4, 5, 6]})
df['k_ts'] = pd.date_range(start='2025-01-01 08:00:00', periods=len(df), freq='S')
df['ts2'] = pd.date_range(start='2025-01-01 12:00:00', periods=len(df), freq='S')
df.set_index('k_ts', inplace=True)
# 配置信息
config = pd.read_csv(Path(__file__).parent / 'my_feat.csv')
df.columns = df.columns.str.strip()
# 注册自定义函数
pd.Series.my_func = (lambda x: x.rolling(3).mean())
# 处理数据
init_pipe()
result = df.extract_features(config)
2.2.3 运行结果
原始数据:
k_device col1 col2 col3
k_ts
2025-01-01 08:00:00 dev1 50 1 2025-01-01 12:00:00
2025-01-01 08:00:01 dev1 60 2 2025-01-01 12:00:01
2025-01-01 08:00:02 dev1 70 3 2025-01-01 12:00:02
2025-01-01 08:00:03 dev2 80 4 2025-01-01 12:00:03
2025-01-01 08:00:04 dev2 90 5 2025-01-01 12:00:04
2025-01-01 08:00:05 dev2 100 6 2025-01-01 12:00:05
结果数据:
feat1 feat10 feat2 feat3 feat4 feat5 feat6 feat7 feat8 feat9
k_ts
2025-01-01 08:00:00 -0.5 6 NaN 1.0 NaN 2 1 0 e 2
2025-01-01 08:00:01 -1.4 6 NaN 1.5 NaN 60 1 0 e 2
2025-01-01 08:00:02 -2.3 6 2.0 2.0 -1.4 70 1 0 e 2
2025-01-01 08:00:03 -3.2 6 3.0 2.5 -2.3 80 1 0 e 2
2025-01-01 08:00:04 -4.1 6 4.0 3.0 -3.2 90 1 0 e 2
2025-01-01 08:00:05 -5.0 6 5.0 3.5 -4.1 100 1 0 e 2
2.2 统一数据格式
提供format_columns()方法,将输入的DataFrame内的k_ts转为时间类型,将k_device列转为字符串类型。
import pandas as pd
from k2pipe.pipe import init_pipe
init_pipe()
df = df.format_columns()
2.3 数据流分析
TODO
2.4 冗余列分析
TODO
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file k2pipe-0.1.0-py3-none-any.whl.
File metadata
- Download URL: k2pipe-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dd5621b220bd0f28887b5f6b8e070821abb9345f9eb592d9edf9983cc1ce740
|
|
| MD5 |
1b93c324301f0ffca8a359ec1bf6dbe3
|
|
| BLAKE2b-256 |
674360c5e38764eddb8e27385a5861f2e43db94b9d1b7ce42079fabedf883afe
|