RFC: Derived Columns
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 28/100
Hướng nghiên cứu
Bắt đầu bằng cách xem xét cơ chế xác thực và tuần tự hóa Schema hiện có, sau đó lần theo decorator @dy.derived được đề xuất và các điểm vào Schema.with_derived(). Giải quyết các câu hỏi còn bỏ ngỏ về các dependency vòng và chuỗi cũng như về tuần tự hóa trước khi triển khai. Công việc được xem là hoàn tất khi các cột dẫn xuất tuân theo hành vi đã nêu đối với đầu vào, việc ghi đè và LazyFrame, đồng thời các target không hợp lệ bị từ chối tại thời điểm định nghĩa class.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Problem
Schemas often include columns that are deterministic functions of other columns. Today, users must compute these outside of dataframely before validation:
df = df.with_columns(
age=(pl.date.today() - pl.col("birth_date")).dt.total_days() // 365
)
validated = PersonSchema.validate(df)
This scatters transformation logic across the codebase and breaks the "schema as source of truth" model.
Proposed API
1. @dy.derived() decorator
Define derived columns alongside rules, using the same pattern:
class PersonSchema(dy.Schema):
birth_date = dy.Date(nullable=False)
first_name = dy.String(nullable=False)
last_name = dy.String(nullable=False)
# Derived columns
age = dy.Int64(nullable=False)
full_name = dy.String(nullable=False)
@dy.derived("age")
def derive_age(cls) -> pl.Expr:
return (pl.date.today() - cls.birth_date.col).dt.total_days() // 365.25
@dy.derived("full_name")
def derive_full_name(cls) -> pl.Expr:
return cls.first_name.col + pl.lit(" ") + cls.last_name.col
2. Schema.with_derived() method
Explicitly apply derivations to a dataframe:
# Input only needs source columns
df = pl.DataFrame({
"birth_date": [date(1990, 5, 15), date(2000, 1, 1)],
"first_name": ["Alice", "Bob"],
"last_name": ["Smith", "Jones"],
})
# Add derived columns
df_with_derived = PersonSchema.with_derived(df)
# Now has: birth_date, first_name, last_name, age, full_name
# Then validate as usual
validated = PersonSchema.validate(df_with_derived)
Expected Behavior
- Derived columns are optional in input.
with_derived()adds them if missing, overwrites if present. - Lazy frames are preserved.
with_derived()returns aLazyFrameif given aLazyFrame. - Invalid targets error at class definition time.
@dy.derived("x")raises ifxis not a column in the schema.
Open Questions
-
Circular dependencies? Should we detect/error on
aderived frombderived froma? If so, this requires topological sorting of derivations—is this doable? -
Chained derivations? Should derived columns be allowed to depend on other derived columns? e.g.,
agederived frombirth_date, thenis_adultderived fromage. This would require ordering derivations correctly (topological sort). -
Serialization? Should derived column expressions be included in
Schema.serialize()? Unclear how this interacts with the existing serialization machinery.
- Ngôn ngữ chính
- Python
- Star
- 618
- Fork
- 21
- Merge trung bình
- 14 giờ 34 phút
- Pull request đã merge (30 ngày)
- 7
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Quantco/dataframely
-
Expose validation errors Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Quantco/dataframely#402 · 3 bình luận ·
-
Adding `sorted` in `dy.Column` Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
Quantco/dataframely#357 · 1 reaction ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
Quantco/dataframely#309 · 4 bình luận · 2 reaction ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 45/100
Quantco/dataframely#295 · 3 bình luận · 1 reaction ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 45/100
Quantco/dataframely#273 · 5 bình luận · 1 reaction ·
Tất cả issue của Quantco/dataframely
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
stephrobert/dsoxlab#238 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
sublimehq/package_control#1780 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
nwg-piotr/nwg-displays#145 ·