SDTM Namespace¶
The df.sdtm namespace maps raw clinical data to CDISC SDTM domains.
fleet.sdtm.SDTMNamespace
¶
Bases: SharedVerbsMixin, PinMixin, FlagCriterionMixin, TrialDesignMixin
Immutable pipeline builder for SDTM derivations.
Each verb appends to a lazy expression list; to_frame() materialises
everything in one with_columns call. Accepts both pl.DataFrame
and pl.LazyFrame inputs, so scanned sources stay lazy end-to-end.
Source code in src/fleet/sdtm/namespace.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 | |
date
¶
date(
target,
*,
source=None,
value=None,
output=None,
time=None,
impute="first",
impute_time=None,
highest=None,
flag=True,
min_date=None,
max_date=None,
format=None
)
Parse, convert, impute, and flag a date or datetime column.
Dispatches behavior based on the CDISC suffix of target (DTC, DT, DTM) or an explicit output override.
For DTC targets: converts raw text to ISO 8601 and never guesses.
A valid ISO 8601 value stays as it is, partial dates included
(SDTMIG 3.4 section 4.4). YYYYMMDD, DD-MON-YYYY,
DD MON YYYY and DDMONYYYY convert with no format. A slash
date or a two-digit year raises until format names the layout.
Any other value becomes null, with one warning at collect that
gives the count and the first values. With time, a blank time
leaves the date alone, a partial date takes a hyphen for each
missing component (2021-03--T08:30), and a time with no date
gives -----T08:30 (SDTMIG 3.4 section 4.4.2). Imputation
parameters are ignored.
For DT/DTM targets: delegates to the imputation engine that
.adam.date() uses, so both give the same result. A
pl.Date or pl.Datetime source is read as its ISO 8601 text;
a number raises, because Fleet does not guess its origin. With
time, the date and the time first become one --DTC value
(2021-03--T08:30 for a partial date), which then takes the
imputation and highest (ADR-0045).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Name of the output column. |
required |
source
|
str | None
|
Column name to read values from. Mutually exclusive with value. |
None
|
value
|
str | None
|
Literal date value to broadcast. Mutually exclusive with source. |
None
|
output
|
str | None
|
Explicit output type: |
None
|
time
|
str | None
|
Time column or literal to combine with date (DTM/DTC). |
None
|
impute
|
str
|
Imputation strategy (DT/DTM only). |
'first'
|
impute_time
|
str | None
|
Time-part imputation override (DTM only). |
None
|
highest
|
str | None
|
Imputation ceiling, as admiral |
None
|
flag
|
bool | str
|
DTF/TMF flag control. |
True
|
min_date
|
str | None
|
Lower bound for an imputed date. Applies only when the bound is inside the possible range of the partial value; a complete date never changes. DT targets only. |
None
|
max_date
|
str | None
|
Upper bound for an imputed date, with the same rule. |
None
|
format
|
str | None
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
The namespace with target appended; call
|
References
SDTMIG 3.4 section 4.4; admiral 1.4.1 derive_vars_dt and
derive_vars_dtm; ADR-0021; ADR-0045.
Examples:
Parse raw date to ISO 8601 DTC::
ae = (
raw_df.sdtm
.date("AESTDTC", source="ae_start_date")
.to_frame()
)
Custom input format::
ae = (
raw_df.sdtm
.date("AEENDTC", source="ae_end_date",
format="%d/%m/%Y")
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
nsv
¶
Derive a Non-Standard Variable with qualifier metadata.
Behaves like assign(source=...) (when source is given) or
assign(value=...) (when value is given) but additionally
registers SUPPQUAL metadata (label, origin, evaluator) on the
pipeline. Use export_supp() to produce the vertical SUPPQUAL
structure for SDTM IG ≤3.4. For IG 3.5+, NSVs remain in the
parent domain via to_frame() unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Name of the qualifier variable (becomes QNAM in SUPP). |
required |
source
|
str | None
|
Name of the raw column to copy from. Mutually exclusive with value. |
None
|
value
|
Any | None
|
A literal value to broadcast. Mutually exclusive with source. |
None
|
label
|
str
|
Human-readable label (becomes QLABEL in SUPP). |
required |
origin
|
str
|
Variable origin (becomes QORIG in SUPP) — the
Define-XML 2.1 vocabulary shared with |
'Derived'
|
evaluator
|
str | None
|
Optional evaluator (becomes QEVAL in SUPP). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
The namespace with target appended; call
|
Examples:
Derive population flag NSVs for DM::
dm = (
raw_df.sdtm
.assign("DOMAIN", value="DM")
.assign("USUBJID", source="patient_number")
.nsv("SAFETY", source="safety_flag",
label="Safety Population Flag")
.nsv("ITT", source="itt_flag",
label="Intent to Treat Population Flag")
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
baseline_flag
¶
baseline_flag(
target,
*,
date_col,
testcd_col=None,
ref_col="RFSTDTC",
ref_pin="DM",
dm=None,
by=None,
result_col=None,
order_by=None
)
Flag the last observation with a result before the reference date as baseline.
Sets target to "Y" for one record (per subject and optional
grouping variables): the last record with a result strictly before
the reference date/time, and null for all other records. A
record on the reference date is eligible only when it and the
reference both have a time and the record time is earlier; a record
on that date with no time is not eligible, because it may follow
the dose (SDTMIG "prior to"; sdtm.oak derive_blfl). Among
eligible records the latest date, then the latest time, wins.
A record has a result when result_col is not null or blank and
--STAT (when present) is not "NOT DONE". When --ORRES is
present and is not result_col, it must hold a value too, unless
--DRVFL is "Y" (CG0553). A tie on the last date and time
goes to the last record by order_by, then input order, as SAS
LAST. after PROC SORT; with no order_by the tie is broken by
--SEQ (when present) and a warning names the groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Name of the output flag variable
(e.g. |
required |
date_col
|
str
|
Column containing the observation date
(e.g. |
required |
testcd_col
|
str | None
|
Optional test code column to group by
(e.g. |
None
|
ref_col
|
str
|
Reference date column in DM (default |
'RFSTDTC'
|
ref_pin
|
str
|
Pinned domain for the reference date
(default |
'DM'
|
dm
|
DataFrame | None
|
Optional explicit DataFrame override. |
None
|
by
|
list[str] | None
|
Additional grouping columns beyond USUBJID and
testcd_col, such as |
None
|
result_col
|
str | None
|
Result column (default |
None
|
order_by
|
str | list[str] | None
|
Column(s) that break a tie on the last date and time
(default |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
The namespace with target appended; call
|
Raises:
| Type | Description |
|---|---|
DerivationError
|
When no result column is found or named. |
References
SDTMIG 3.4, --LOBXFL: the last non-missing value prior to
RFXSTDTC; CDISC CORE FB2603/FB2604 (one flag per group);
admiral ad_advs.R order (ADT, VISITNUM, VSSEQ).
Examples:
Flag baseline vital signs::
vs = (
vs_raw.sdtm
.assign("DOMAIN", value="VS")
.assign("USUBJID", source="patient_number")
.assign("VSTESTCD", source="vstestcd")
.assign("VSORRES", source="vsresult")
.date("VSDTC", source="vs_date")
.pin("DM", dm_df)
.baseline_flag("VSBLFL", date_col="VSDTC",
testcd_col="VSTESTCD")
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | |
condition_add
¶
Set target to then_value where condition is true, else null.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Name of the output SDTM variable. |
required |
condition
|
str | Expr
|
A Polars expression (or string) that evaluates to a boolean mask. |
required |
then_value
|
Any
|
The value to assign when condition is true. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
The namespace with target appended; call
|
Examples:
Set AEOUT to "FATAL" where the death flag is set::
ae = (
raw_df.sdtm
.condition_add(
"AEOUT",
condition='pl.col("death_flag") == "Y"',
then_value="FATAL",
)
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
ae_aerel
¶
Map free-text AE causality strings to AEREL values.
Mapping is case-insensitive against aerel_map. The default map
is Fleet's own convention, not CDISC terminology: SDTM IG v3.4
gives AEREL no codelist, so the sponsor defines the terms. The
AE relationship table (AERelationshipSpec.relationship_order)
must list the same terms; a term it does not list stops the
table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Output column name (default |
'AEREL'
|
source
|
str
|
Raw column containing causality strings. |
'ae_relationship'
|
aerel_map
|
dict[str, str] | None
|
Optional |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
The namespace with target appended; call
|
References
SDTM IG v3.4, AE domain, AEREL (no controlled terminology).
Examples:
Derive AEREL from raw causality using the default mapping::
ae = (
raw_df.sdtm
.ae_aerel("AEREL", source="ae_relationship")
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
seq
¶
Derive the --SEQ sequence number variable.
Generates a 1-based integer sequence that uniquely identifies each
record within the domain, per subject. The dataset is sorted by
order_by columns within each by partition before numbering
— this follows the sdtm.oak derive_seq() pattern and ensures
deterministic, reproducible sequence values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Name of the output |
required |
order_by
|
list[str] | None
|
Columns that define the sort order within each group.
Records that tie keep their input order, as PROC SORT
EQUALS. When |
None
|
by
|
list[str] | None
|
Subject-level grouping columns (default
|
None
|
start_at
|
int
|
First sequence value (default |
1
|
check_uniqueness
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
A fresh namespace with target appended. Call
|
Examples:
Derive AESEQ ordered by start date within each subject::
ae = (
raw_df.sdtm
.assign("DOMAIN", value="AE")
.assign("USUBJID", source="patient_number")
.assign("AETERM", source="ae_term")
.assign("AESTDTC", source="ae_start_date")
.seq("AESEQ", order_by=["AESTDTC"])
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
group_by
¶
Group by by and apply agg aggregations (lazily).
The groups come out in the order of their first record, so the same input gives the same row order at each run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
list[str]
|
Column(s) to group by. |
required |
agg
|
list[Expr]
|
List of Polars aggregation expressions. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
A fresh namespace continuing from the grouped plan. |
Examples:
Compute max vital-sign result per subject and test::
vs_agg = (
raw_df.sdtm
.group_by(
by=["USUBJID", "VSTESTCD"],
agg=[pl.col("VSSTRESN").max().alias("AVAL")],
)
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
notsubmitted
¶
Mark one or more variables as not submitted (set to null).
Useful to explicitly null out eCRF fields that are not included in the SDTM submission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*targets
|
str
|
One or more variable names to null out. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
The namespace with each target set to null;
call |
Examples:
Null out internal scratch fields before submission::
dm = (
raw_df.sdtm
.assign("USUBJID", source="subject_id")
.notsubmitted("_raw_id", "_site_code")
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
multiple_responses
¶
Explode a delimited multi-response field into separate rows.
Splits source on delimiter and explodes — lazily, as part of the accumulated plan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Name of the output column for the split values. |
required |
source
|
str
|
Name of the raw column containing delimited responses. |
required |
delimiter
|
str
|
Separator between responses (default |
'|'
|
Returns:
| Name | Type | Description |
|---|---|---|
SDTMNamespace |
SDTMNamespace
|
A fresh namespace with one row per response. |
Examples:
Explode pipe-delimited AE terms into separate rows::
ae = (
raw_df.sdtm
.multiple_responses(
"AETERM", source="ae_terms", delimiter="|",
)
.to_frame()
)
Source code in src/fleet/sdtm/namespace.py
export_supp
¶
Export Non-Standard Variables as a vertical SUPPQUAL DataFrame.
Produces the CDISC SUPPQUAL structure (STUDYID, RDOMAIN, USUBJID, IDVAR, IDVARVAL, QNAM, QLABEL, QVAL, QORIG, QEVAL) for SDTM IG ≤3.4 submissions.
Resolution order (first match wins):
- qualifiers dict — explicit metadata per qualifier column.
- spec + dataset — reads
role: qualifiervariables from adefine.yaml. - Pipeline metadata — uses
_nsv_metapopulated by priornsv()calls in the chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qualifiers
|
dict[str, dict[str, str]] | None
|
Explicit qualifier metadata mapping. Keys are
column names (QNAM), values are dicts with |
None
|
spec
|
str | Path | None
|
Path to a |
None
|
dataset
|
str | None
|
Dataset name within the spec (e.g. |
None
|
idvar
|
str | None
|
Name of the identifying variable column (e.g.
|
None
|
domain
|
str | None
|
Explicit RDOMAIN value. When |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pl.DataFrame: Vertical SUPPQUAL frame with columns STUDYID,
RDOMAIN, USUBJID, IDVAR, IDVARVAL, QNAM, QLABEL, QVAL,
QORIG, QEVAL. A null or blank value gives no record. A
float is written with no |
Raises:
| Type | Description |
|---|---|
DerivationError
|
When no source gives qualifier metadata, a qualifier or idvar is not a column, idvar is missing for a domain other than DM, RDOMAIN is unknown, or the spec dataset is not found. |
Examples:
Export from pipeline metadata (Pattern A)::
ns = (
raw_df.sdtm
.assign("DOMAIN", value="DM")
.assign("USUBJID", source="patient_number")
.nsv("SAFETY", source="safety_flag",
label="Safety Population Flag")
)
suppdm = ns.export_supp()
Export from define.yaml spec (Pattern D)::
dm = raw_df.sdtm.from_spec("define.yaml", dataset="DM").to_frame()
suppdm = dm.sdtm.export_supp(spec="define.yaml", dataset="DM")
Export an AE qualifier linked by AESEQ::
suppae = ae.sdtm.export_supp(
qualifiers={"AETOXGR": {"label": "AE Toxicity Grade"}},
idvar="AESEQ",
)
Export with explicit qualifier dict (Pattern C)::
suppdm = dm.sdtm.export_supp(
qualifiers={
"COMPLT16": {"label": "Completers of Week 16",
"origin": "Derived"},
"SAFETY": {"label": "Safety Population Flag",
"origin": "Derived"},
},
)
Source code in src/fleet/sdtm/namespace.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 | |
options: show_root_heading: true heading_level: 2 members_order: source inherited_members: true filters: ["!^_"]