Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

darwin-cleaner

契约与规则驱动的单文件清洗引擎:读取一个 parquet(或内存 DataFrame)→ 行分叉 / 列分叉 → 按 order 跑 middleware → 在内存中返回数据与 meta。

边界

本库只做数据变换,不做上下游事务:

库内 库外(上层应用)
读单个 parquet / 接收 DataFrame 多文件合并、文件发现、新鲜度判定
行分叉 / 列分叉 加载 cleaner.json / clean.d/*.json / adapter 配置
12 个内置 middleware(含默认关闭的 dedupe) 写 parquet / csv / meta / 断点
契约与规则校验、统计与报告生成 CLI、调度、退出码、业务标识

详细设计见 docs/design.md,现有代码梳理见 docs/clean阶段代码逻辑梳理.md。

安装

pip install -e ".[test]"

依赖:pandas>=2.0、pyarrow>=12;Python >= 3.10。

快速开始

from pathlib import Path
from darwin_cleaner import (
    CleaningEngine, CleanRequest, CleanOptions, Contract, FieldSpec,
    MiddlewareOverride, MiddlewareRegistryImpl, Rules, TypeSpec,
)

registry = MiddlewareRegistryImpl()
registry.builtins()  # 12 个内置步骤,dedupe 默认关闭

contract = Contract(
    name="stk_ohlcv",
    fields={
        "symbol": FieldSpec(name="symbol", type="str"),
        "trade_date": FieldSpec(name="trade_date", type="str"),
        "close": FieldSpec(name="close", type="float"),
    },
    types={"float": TypeSpec(name="float", converter="numeric", min=0)},
    output=("symbol", "trade_date", "close"),
    keys=("symbol", "trade_date"),  # 参与 keys 的列自动 required
)

engine = CleaningEngine(registry)
result = engine.clean(CleanRequest(
    input=Path("raw.parquet"),
    contracts={"stk_ohlcv": contract},
    rules=Rules(),
    options=CleanOptions(
        middleware_overrides={
            "dedupe": MiddlewareOverride(
                enabled=True,
                params={"scope": "keys", "strategy": "last"},
            ),
        },
    ),
))

for out in result.outputs:
    if out.status == "success":
        df = out.data        # pandas.DataFrame
        meta = out.meta      # 内容 hash + 规则指纹 + 统计
        report = out.report  # 过程审计
        # 写盘由上层负责

核心概念

  • Contract:字段、类型、输出列、唯一行键、排序。由上层解析配置后注入,引擎不读配置文件。
  • FieldSpec.required:三态。True 缺失即报错;False 显式允许缺失(补空列);None 未声明时由引擎推导——参与 sort / keys 的列自动为 True,其余为 False。
  • Rules:mapping / value_map / transform / filters / outputs 等,替代 middleware 内的全局配置读取。
  • Middleware:统一签名 (df, ctx, params) -> df | None。order 决定执行顺序,params 是声明式参数(计入规则指纹),enabled 是开关;MiddlewareOverride 可按请求覆盖。
  • order 分段:10-19 预处理、20-29 dtype、30-39 填充、40-49 公式、50-69 校验/去重、90-99 输出。

内置 middleware

order 名称 作用 默认
10 field_mapping 字段改名 开
12 value_mapping 值替换 开
15 field_transform 常量 / 系数 / 动作 开
20 type_conversion dtype 归一 开
30 fill 分组 ffill 开
40 formula_compute 全空列按公式计算 开
45 formula_validate 双容差公式校验 开
50 consistency 字段间一致性 开
60 type_validate inf / min / max 越界置空 开
70 dedupe 按 keys 去重 关
90 output 按契约选列 开
95 sort 按契约排序 开

测试

pytest

许可证

MIT

Metadata

Release files for darwin-cleaner 0.4.0.dev0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for darwin-cleaner 0.4.0.dev0
File Size Uploaded Provenance Actions
darwin_cleaner-0.4.0.dev0.tar.gz 28.5 kB Without provenance Details for darwin_cleaner-0.4.0.dev0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for darwin-cleaner 0.4.0.dev0
File Interpreter ABI Platform Actions
darwin_cleaner-0.4.0.dev0-py3-none-any.whl Python 3 none any Details for darwin_cleaner-0.4.0.dev0-py3-none-any.whl

Total release size: 55.9 kB

Release files / darwin_cleaner-0.4.0.dev0.tar.gz

Download URL darwin_cleaner-0.4.0.dev0.tar.gz
Size 28.5 kB
Tags Source
SHA-256 checksum
How to use checksums
63f4498b2c79bbbe1f4a5d1b8f2b9bf70263a54713ecc44ffde9b07fbe4a4038
BLAKE2b-256 checksum
How to use checksums
49b5ddf3cd6c15fa57ab8485d393003d18ba6dcfcea6e53ad20522a062a02414
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / darwin_cleaner-0.4.0.dev0-py3-none-any.whl

Download URL darwin_cleaner-0.4.0.dev0-py3-none-any.whl
Size 27.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39cd40e550cc79747e93339627734d55d214e20b849de9bb1c541ed290b2ef14
BLAKE2b-256 checksum
How to use checksums
fcb06134ed7bebf1b59b1d8dabeaf8fa2010c757042f45734a0ae0c5684337fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for darwin-cleaner 0.4.0.dev0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page