GitHub Actions自动化数据质量检测:fg-data-profiling 持续集成配置终极指南 [特殊字符]
GitHub Actions自动化数据质量检测:fg-data-profiling 持续集成配置终极指南 🚀
在数据驱动的时代,数据质量是每个项目的生命线。fg-data-profiling(原名 ydata-profiling)作为一款强大的数据质量分析和探索性数据分析工具,能够通过一行代码为Pandas和Spark DataFrame提供全面的数据质量报告。今天,我将为你展示如何将 fg-data-profiling 与 GitHub Actions 结合,构建自动化的数据质量检测流水线,确保每次代码提交都能自动进行数据质量检查!✨
🔍 为什么需要自动化数据质量检测?
在数据科学和机器学习项目中,数据质量问题常常被忽视,导致模型效果不佳或分析结果偏差。传统的手动检查不仅耗时,而且容易遗漏问题。通过 GitHub Actions 集成 fg-data-profiling,你可以:
- 自动发现数据异常:每次提交代码时自动检测缺失值、异常值、数据分布等问题
- 持续监控数据质量:跟踪数据质量随时间的变化趋势
- 团队协作标准化:确保所有团队成员遵循相同的数据质量标准
- 及早发现问题:在数据问题影响生产环境前及时预警
🛠️ fg-data-profiling 核心功能速览
fg-data-profiling 生成的交互式数据质量分析报告
fg-data-profiling 提供以下关键功能:
- 🔍 类型推断:自动识别列的数据类型(分类、数值、日期等)
- ⚠️ 智能警告:自动汇总数据问题(缺失值、不准确、偏斜等)
- 📊 单变量分析:描述性统计和分布直方图
- 🔗 多变量分析:相关性分析、缺失数据详细分析
- ⏰ 时间序列分析:自相关、季节性分析
- 📄 文本分析:常见类别、脚本和块分析
🚀 快速配置 GitHub Actions 数据质量检测
步骤1:创建 GitHub Actions 工作流文件
在你的项目根目录下创建 .github/workflows/data-quality.yml 文件:
name: Data Quality Check
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
jobs:
data-quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install fg-data-profiling pandas numpy
# 如果你的项目有 requirements.txt
# pip install -r requirements.txt
- name: Run data quality profiling
run: |
python -c "
import pandas as pd
from data_profiling import ProfileReport
# 加载你的数据文件
df = pd.read_csv('data/your_dataset.csv')
# 生成数据质量报告
profile = ProfileReport(df, title='Data Quality Report')
# 保存报告
profile.to_file('data_quality_report.html')
# 也可以保存为JSON用于自动化检查
profile.to_file('data_quality_report.json')
print('数据质量报告已生成!')
"
- name: Upload data quality report
uses: actions/upload-artifact@v4
with:
name: data-quality-report
path: data_quality_report.html
步骤2:配置数据质量阈值检查
为了自动化检查数据质量,你可以在工作流中添加质量阈值检查:
- name: Check data quality metrics
run: |
python -c "
import pandas as pd
from data_profiling import ProfileReport
import json
df = pd.read_csv('data/your_dataset.csv')
profile = ProfileReport(df, minimal=True)
report = profile.to_json()
# 解析JSON报告
data = json.loads(report)
# 检查关键指标
missing_percentage = data['table']['n_cells_missing'] / data['table']['n'] * 100
duplicate_rows = data['table']['n_duplicates']
print(f'缺失值比例: {missing_percentage:.2f}%')
print(f'重复行数: {duplicate_rows}')
# 设置质量阈值
if missing_percentage > 10:
print('⚠️ 警告:缺失值比例超过10%')
exit(1)
if duplicate_rows > 0:
print('⚠️ 警告:发现重复行')
exit(1)
print('✅ 数据质量检查通过')
"
📊 高级配置:多环境数据质量测试
支持多种Python版本和Pandas版本
参考 fg-data-profiling 项目的 tests.yml 配置,我们可以创建更全面的测试矩阵:
name: Multi-environment Data Quality Check
on:
pull_request:
push:
branches:
- main
- develop
env:
YDATA_PROFILING_NO_ANALYTICS: false
jobs:
data-quality-matrix:
name: Data Quality Tests
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.10", "3.11", "3.12", "3.13"]
pandas: ["pandas>1.1", "pandas>2.0"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ matrix.pandas }}
- name: Install dependencies
run: |
pip install --upgrade pip setuptools wheel
pip install "fg-data-profiling[notebook]" "${{ matrix.pandas }}" "numpy>=1.21"
- name: Run comprehensive data profiling
run: |
python scripts/data_quality_check.py
Spark 数据质量检查配置
如果你的项目使用 Spark,可以参考项目的 Spark测试配置:
test-spark-data-quality:
runs-on: ubuntu-latest
continue-on-error: false
strategy:
matrix:
include:
- { python-version: "3.10", pyspark-version: "3.5" }
- { python-version: "3.11", pyspark-version: "4.0" }
name: Spark Data Quality | Python ${{ matrix.python-version }} | PySpark ${{ matrix.pyspark-version }}
steps:
- uses: actions/checkout@v4
- name: Set up Java 17
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Spark dependencies
run: |
pip install "pyspark~=${{ matrix.pyspark-version }}" "pyarrow>4.0.0"
pip install "fg-data-profiling[spark]"
- name: Run Spark data profiling
run: |
python scripts/spark_data_quality.py
📈 数据质量报告自动化生成与存档
生成HTML和JSON报告
- name: Generate and archive reports
run: |
python -c "
import pandas as pd
from data_profiling import ProfileReport
from datetime import datetime
# 加载数据
df = pd.read_csv('data/production_data.csv')
# 生成详细报告
profile = ProfileReport(
df,
title=f'Data Quality Report - {datetime.now().strftime(\"%Y-%m-%d\")}',
explorative=True
)
# 保存多种格式
profile.to_file('reports/data_quality_report.html')
profile.to_file('reports/data_quality_report.json')
# 生成简化版报告(用于快速查看)
profile_minimal = ProfileReport(df, minimal=True)
profile_minimal.to_file('reports/data_quality_summary.html')
print('报告已生成到 reports/ 目录')
"
- name: Upload all reports
uses: actions/upload-artifact@v4
with:
name: data-quality-reports
path: |
reports/data_quality_report.html
reports/data_quality_report.json
reports/data_quality_summary.html
集成到Pull Request流程
你可以在Pull Request中自动添加数据质量检查结果:
- name: Comment PR with data quality summary
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
try {
const report = JSON.parse(fs.readFileSync('reports/data_quality_report.json', 'utf8'));
const summary = `
## 📊 数据质量检查结果
**数据集概览**
- 总行数: ${report.table.n}
- 总列数: ${report.table.n_var}
- 缺失值比例: ${(report.table.n_cells_missing / report.table.n * 100).toFixed(2)}%
**数据质量警告**
${report.alerts.map(alert => `- ⚠️ ${alert.alert_type}: ${alert.description}`).join('\n')}
**建议操作**
${report.alerts.length > 0 ?
'请检查上述数据质量问题并进行相应处理。' :
'✅ 数据质量良好,无重大问题。'}
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: summary
});
} catch (error) {
console.log('无法生成数据质量评论:', error);
}
🔧 实用技巧与最佳实践
1. 配置文件质量检查脚本
创建 scripts/data_quality_check.py:
#!/usr/bin/env python3
"""
数据质量检查脚本 - 集成到CI/CD流水线
"""
import pandas as pd
from data_profiling import ProfileReport
import json
import sys
def check_data_quality(data_path, thresholds=None):
"""检查数据质量并返回结果"""
if thresholds is None:
thresholds = {
'max_missing_percentage': 10,
'max_duplicate_rows': 0,
'max_skewness': 3
}
# 加载数据
df = pd.read_csv(data_path)
# 生成报告
profile = ProfileReport(df, minimal=True)
report = json.loads(profile.to_json())
# 提取关键指标
metrics = {
'missing_percentage': report['table']['n_cells_missing'] / report['table']['n'] * 100,
'duplicate_rows': report['table']['n_duplicates'],
'variables': report['table']['n_var'],
'alerts': len(report.get('alerts', []))
}
# 检查阈值
issues = []
if metrics['missing_percentage'] > thresholds['max_missing_percentage']:
issues.append(f"缺失值比例过高: {metrics['missing_percentage']:.2f}%")
if metrics['duplicate_rows'] > thresholds['max_duplicate_rows']:
issues.append(f"发现重复行: {metrics['duplicate_rows']}")
return {
'passed': len(issues) == 0,
'metrics': metrics,
'issues': issues,
'report_path': 'data_quality_report.html'
}
if __name__ == "__main__":
result = check_data_quality('data/your_dataset.csv')
if result['passed']:
print("✅ 数据质量检查通过")
sys.exit(0)
else:
print("❌ 数据质量检查失败:")
for issue in result['issues']:
print(f" - {issue}")
sys.exit(1)
2. 使用缓存优化性能
- name: Cache data profiling results
uses: actions/cache@v4
with:
path: |
~/.cache/data_profiling
reports/
key: ${{ runner.os }}-data-profiling-${{ hashFiles('data/*.csv') }}
restore-keys: |
${{ runner.os }}-data-profiling-
📋 完整示例:端到端数据质量流水线
以下是一个完整的 .github/workflows/data-quality-pipeline.yml 示例:
name: Data Quality Pipeline
on:
schedule:
- cron: '0 8 * * *' # 每天8点运行
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
data-quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install fg-data-profiling[notebook] pandas numpy
- name: Run data quality checks
run: |
python scripts/run_quality_checks.py
- name: Generate detailed report
run: |
python scripts/generate_detailed_report.py
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: data-quality-artifacts
path: |
reports/*.html
reports/*.json
logs/
- name: Notify on failure
if: failure()
uses: actions/github-script@v7
with:
script: |
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: '数据质量检查失败',
body: '请检查最新提交的数据质量问题。',
labels: ['data-quality', 'bug']
})
🎯 总结
通过将 fg-data-profiling 集成到 GitHub Actions,你可以构建一个强大的自动化数据质量检测系统。这种方法不仅提高了数据质量的可视化程度,还确保了数据质量检查的持续性和一致性。
关键优势:
- ✅ 自动化检测:每次提交自动运行数据质量检查
- ✅ 早期预警:在问题进入生产环境前发现
- ✅ 团队协作:统一的数据质量标准
- ✅ 历史追踪:跟踪数据质量随时间的变化
- ✅ 灵活配置:支持多种数据格式和分析需求
开始使用 fg-data-profiling 和 GitHub Actions 来提升你的数据质量保障水平吧!如果你在配置过程中遇到任何问题,可以参考项目的 官方文档 或查看 测试配置 获取更多灵感。
记住,好的数据质量是成功的数据科学项目的基础!📊🔍
更多推荐









所有评论(0)