Skip to content

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
@pl.api.register_dataframe_namespace("sdtm")
@pl.api.register_lazyframe_namespace("sdtm")
class SDTMNamespace(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.
    """

    # -- SharedVerbsMixin configuration --
    _STUDY_DAY_REF_COL: str = "RFSTDTC"
    _STUDY_DAY_REF_PIN: str = "DM"
    _SEQ_DEFAULT_GROUPS: list[str] = ["STUDYID", "USUBJID"]

    _FK_RULES: dict[str, dict[str, str]] = {
        "USUBJID": {"pin": "DM", "ref_col": "USUBJID"},
        "VISITNUM": {"pin": "TV", "ref_col": "VISITNUM"},
        "VISIT": {"pin": "TV", "ref_col": "VISIT"},
        "EPOCH": {"pin": "TE", "ref_col": "EPOCH"},
        "ETCD": {"pin": "TE", "ref_col": "ETCD"},
    }

    def __init__(self, df: pl.DataFrame | pl.LazyFrame) -> None:
        self._lazy = df.lazy()
        self._root_lazy = self._lazy  # the start of the chain, for to_spec_yaml()
        self._nsv_meta: list[NSVMeta] = []
        self._domain: str | None = None
        self._pinned: dict[str, tuple[pl.DataFrame, list[str]]] = {}
        self._pin_lineage: list = []
        self._pending_fk_checks: list[tuple[str, str, str, str]] = []

    # ------------------------------------------------------------------ #
    # assign / case_when are inherited from SharedVerbsMixin.             #
    # ------------------------------------------------------------------ #

    # ------------------------------------------------------------------ #
    # 3. date — unified date verb                                          #
    # ------------------------------------------------------------------ #

    def date(
        self,
        target: str,
        *,
        source: str | None = None,
        value: str | None = None,
        output: str | None = None,
        time: str | None = None,
        impute: str = "first",
        impute_time: str | None = None,
        highest: str | None = None,
        flag: bool | str = True,
        min_date: str | None = None,
        max_date: str | None = None,
        format: str | None = None,
    ) -> SDTMNamespace:
        """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).

        Args:
            target: Name of the output column.
            source: Column name to read values from.  Mutually exclusive
                with *value*.
            value: Literal date value to broadcast.  Mutually exclusive
                with *source*.
            output: Explicit output type: ``"dtc"``, ``"dt"``, or ``"dtm"``.
            time: Time column or literal to combine with date (DTM/DTC).
            impute: Imputation strategy (DT/DTM only).
            impute_time: Time-part imputation override (DTM only).
            highest: Imputation ceiling, as admiral ``highest_imputation``:
                ``"n"``, ``"D"`` or ``"M"``, and on a DTM target also
                ``"h"``, ``"m"`` and ``"s"``.  ``"Y"`` raises.
            flag: DTF/TMF flag control.
            min_date: 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.
            max_date: Upper bound for an imputed date, with the same rule.
            format: ``strptime`` layout of the raw text (DTC only), e.g.
                ``"%m/%d/%Y"``.  Use ``%y`` for a two-digit year: it reads
                00-69 as 20xx and 70-99 as 19xx.

        Returns:
            SDTMNamespace: The namespace with *target* appended; call
                ``.to_frame()`` to materialise.

        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()
                )
        """
        from fleet.adam.utils import (
            _resolve_output_type,
            dtm_source_text,
            impute_and_flag,
            source_text,
        )

        # Validate source / value mutual exclusivity
        if source is not None and value is not None:
            raise DerivationError("Cannot specify both source= and value=. Choose one.")
        if source is None and value is None:
            raise DerivationError("Must specify either source= or value=.")

        output_type = _resolve_output_type(target, output)

        # value= path
        if value is not None:
            if flag is True:
                flag = False
            exprs = impute_and_flag(None, target, value=value, output_type=output_type, flag=flag)
            return self._chain(*exprs, verb="date")

        # source= path
        assert source is not None

        if output_type == "dtc":
            return self._dtc(target, source, time=time, format=format)

        # DT/DTM: delegate to imputation engine
        if time is not None and output_type != "dtm":
            raise DerivationError(
                f"time= parameter is only valid for DTM targets, "
                f"but target '{target}' resolves to output='{output_type}'."
            )
        schema = self._lazy.collect_schema()
        if time is not None:
            src = dtm_source_text(
                schema, source=source, target=target, time=time, input_format=format
            )
        else:
            src = source_text(source, schema.get(source), verb=f"date('{target}')")
        exprs = impute_and_flag(
            src,
            target,
            output_type=output_type,
            impute=impute,
            impute_time=impute_time,
            highest=highest,
            min_date=min_date,
            max_date=max_date,
            flag=flag,
        )
        return self._chain(*exprs, verb="date")

    def _dtc(
        self, target: str, source: str, *, time: str | None, format: str | None
    ) -> SDTMNamespace:
        """``--DTC`` text from raw text: ISO 8601 or null, never a guess, with warnings."""
        raw = pl.col(source)
        guessed = guessed_values(raw, input_format=format).fill_null(False)
        sample = (
            self._lazy.select(raw.cast(pl.String).filter(guessed).unique(maintain_order=True))
            .head(3)
            .collect()
            .to_series()
            .to_list()
        )
        if sample and format is None:
            raise DerivationError(
                f"date('{target}'): {source} needs a guess to become a date (the order of day "
                "and month, or the century of a two-digit year). Name the layout with "
                "format='%m/%d/%Y' (month first) or format='%d/%m/%Y' (day first); for a "
                "two-digit year use %y, which reads 00-69 as 20xx and 70-99 as 19xx. "
                f"Values: {sample}"
            )
        if sample:
            raise DerivationError(
                f"date('{target}', format='{format}'): {source} gives a year before 1000 with "
                f"this layout, so the year has two digits: use %y in place of %Y. Values: {sample}"
            )

        text = dtc_text(raw, input_format=format)
        given = raw.cast(pl.String).str.strip_chars().fill_null("") != ""
        value = warn_on_collect(
            text,
            given & text.is_null(),
            lambda n, values: (
                f"date('{target}'): {n} value(s) of {source} are not ISO 8601 and could not be "
                f"converted, so {target} is null for them (SDTMIG 3.4 section 4.4.1). Name the "
                f"layout with format=. Values: {values}"
            ),
            dtype=pl.String,
            shown=raw.cast(pl.String),
        )
        if time is not None:
            raw_time = pl.lit(time) if TIME_LITERAL.match(time) else pl.col(time)
            value = warn_on_collect(
                dtc_with_time(value, raw, raw_time),
                bad_time(raw_time),
                lambda n, values: (
                    f"date('{target}'): {n} time value(s) of {time} are not hh:mm[:ss], so "
                    f"{target} keeps the date only. Values: {values}"
                ),
                dtype=pl.String,
                shown=raw_time.cast(pl.String),
            )
        return self._chain(value.alias(target), verb="date")

    # ------------------------------------------------------------------ #
    # 5b. nsv — Non-Standard Variable (qualifier) derivation               #
    # ------------------------------------------------------------------ #

    def nsv(
        self,
        target: str,
        *,
        source: str | None = None,
        value: Any | None = None,
        label: str,
        origin: str = "Derived",
        evaluator: str | None = None,
    ) -> SDTMNamespace:
        """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.

        Args:
            target: Name of the qualifier variable (becomes QNAM in SUPP).
            source: Name of the raw column to copy from.  Mutually
                exclusive with *value*.
            value: A literal value to broadcast.  Mutually exclusive with
                *source*.
            label: Human-readable label (becomes QLABEL in SUPP).
            origin: Variable origin (becomes QORIG in SUPP) — the
                Define-XML 2.1 vocabulary shared with ``origin:`` in a
                define.yaml (``Collected``, ``Derived``, ``Assigned``,
                ``Protocol``, ``Predecessor``).
            evaluator: Optional evaluator (becomes QEVAL in SUPP).

        Returns:
            SDTMNamespace: The namespace with *target* appended; call
                ``.to_frame()`` to materialise.

        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()
                )
        """
        if source is None and value is None:
            raise DerivationError("nsv() requires either 'source' or 'value'.")
        if source is not None and value is not None:
            raise DerivationError("nsv() accepts 'source' or 'value', not both.")

        expr = pl.col(source).alias(target) if source else pl.lit(value).alias(target)
        new = self._chain(expr, verb="nsv")
        # Copy-on-write: avoid mutating shared list across copies
        new._nsv_meta = [
            *self._nsv_meta,
            NSVMeta(target=target, label=label, origin=origin, evaluator=evaluator),
        ]
        return new

    # ------------------------------------------------------------------ #
    # 5c. study_day is inherited from SharedVerbsMixin (one shared        #
    # implementation; `dm=` / `adsl=` are documented ref_df aliases).     #
    # ------------------------------------------------------------------ #

    # ------------------------------------------------------------------ #
    # 5d. baseline_flag — last pre-reference-date observation flag        #
    # ------------------------------------------------------------------ #

    def baseline_flag(
        self,
        target: str,
        *,
        date_col: str,
        testcd_col: str | None = None,
        ref_col: str = "RFSTDTC",
        ref_pin: str = "DM",
        dm: pl.DataFrame | None = None,
        by: list[str] | None = None,
        result_col: str | None = None,
        order_by: str | list[str] | None = None,
    ) -> SDTMNamespace:
        """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.

        Args:
            target: Name of the output flag variable
                (e.g. ``"VSBLFL"``).
            date_col: Column containing the observation date
                (e.g. ``"VSDTC"``).
            testcd_col: Optional test code column to group by
                (e.g. ``"VSTESTCD"``).  When set, baseline is
                computed independently for each test.
            ref_col: Reference date column in DM (default ``"RFSTDTC"``).
            ref_pin: Pinned domain for the reference date
                (default ``"DM"``).
            dm: Optional explicit DataFrame override.
            by: Additional grouping columns beyond USUBJID and
                *testcd_col*, such as ``--CAT``, ``--SCAT``, ``--SPEC`` and
                ``--METHOD``.
            result_col: Result column (default ``--STRESC`` when present,
                else ``--ORRES``; the prefix comes from *target*).
            order_by: Column(s) that break a tie on the last date and time
                (default ``--SEQ`` when present).

        Returns:
            SDTMNamespace: The namespace with *target* appended; call
                ``.to_frame()`` to materialise.

        Raises:
            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()
                )
        """
        import warnings

        from fleet.core.lineage import record_pin_lineage

        df = self._lazy.collect()

        prefix = next(
            (target[: -len(s)] for s in ("LOBXFL", "BLFL") if target.endswith(s) and target != s),
            target[:2],
        )
        if result_col is None:
            candidates = (f"{prefix}STRESC", f"{prefix}ORRES")
            result_col = next((c for c in candidates if c in df.columns), None)
            if result_col is None:
                raise DerivationError(
                    f"baseline_flag('{target}'): neither {candidates[0]} nor {candidates[1]} "
                    "is in the frame. Name the result column with result_col=."
                )
        elif result_col not in df.columns:
            raise DerivationError(
                f"baseline_flag('{target}'): result_col={result_col!r} is not in the frame."
            )

        # Resolve reference date
        if dm is not None:
            ref_df = dm
            join_key = ["USUBJID"]
        elif ref_col in df.columns:
            ref_df = None
            join_key = []
        else:
            ref_df = self._require_pin(ref_pin.upper(), "baseline_flag", columns=[ref_col])
            join_key = self._pin_join_key(ref_pin.upper())

        # Join reference if needed
        _ref_tmp = f"__pin_{ref_col}__"
        if ref_df is not None:
            from fleet.core.joins import refuse_repeated_keys

            ref_slim = ref_df.select(
                [c for c in dict.fromkeys(join_key + [ref_col]) if c in ref_df.columns]
            )
            if join_key:
                ref_slim = refuse_repeated_keys(
                    "baseline_flag()",
                    ref_slim,
                    join_key,
                    source="DM" if dm is not None else ref_pin.upper(),
                )
            ref_slim = ref_slim.rename({ref_col: _ref_tmp})
            df = df.join(ref_slim, on=join_key, how="left", nulls_equal=True)
            ref_col_name = _ref_tmp
        else:
            ref_col_name = ref_col

        # Build grouping keys
        grp_keys = ["USUBJID"]
        if testcd_col is not None:
            grp_keys.append(testcd_col)
        if by:
            grp_keys.extend(by)
        grp_keys = list(dict.fromkeys(grp_keys))

        # Last record strictly before the reference (Q1 B, 2026-11-02): on the reference
        # date a record is eligible only when both times are known and it is earlier.
        def day_and_time(name: str) -> tuple[pl.Expr, pl.Expr]:
            dtype = df.schema[name]
            if dtype == pl.String:
                return date_part(pl.col(name)), datetime_part(pl.col(name))
            if isinstance(dtype, pl.Datetime):
                return pl.col(name).cast(pl.Date), pl.col(name).cast(pl.Datetime("us"))
            return pl.col(name), pl.lit(None, dtype=pl.Datetime("us"))

        day, time = day_and_time(date_col)
        ref_day, ref_time = day_and_time(ref_col_name)
        before_ref = (day < ref_day) | ((day == ref_day) & (time < ref_time))

        has_result = pl.col(result_col).is_not_null()
        if df.schema[result_col] == pl.String:
            has_result = has_result & (pl.col(result_col).str.strip_chars() != "")
        stat_col, orres_col, drvfl_col = f"{prefix}STAT", f"{prefix}ORRES", f"{prefix}DRVFL"
        if stat_col in df.columns:
            has_result = has_result & (pl.col(stat_col).fill_null("") != "NOT DONE")
        if orres_col in df.columns and orres_col != result_col:
            orres_ok = pl.col(orres_col).cast(pl.String).str.strip_chars().fill_null("") != ""
            if drvfl_col in df.columns:
                orres_ok = orres_ok | (pl.col(drvfl_col).fill_null("") == "Y")
            has_result = has_result & orres_ok

        eligible = before_ref.fill_null(False) & has_result.fill_null(False)
        key = pl.coalesce(time, day.cast(pl.Datetime("us")))
        df = df.with_row_index("__bl_row__").with_columns(
            pl.when(eligible).then(key).alias("__bl_key__"),
        )

        if order_by is not None:
            tie_cols = [order_by] if isinstance(order_by, str) else list(order_by)
        else:
            tie_cols = [c for c in (f"{prefix}SEQ",) if c in df.columns]
        last_day = (
            df.filter(pl.col("__bl_key__").is_not_null())
            .filter(pl.col("__bl_key__") == pl.col("__bl_key__").max().over(grp_keys))
            .sort(["__bl_key__", *tie_cols, "__bl_row__"], descending=True, nulls_last=True)
            .group_by(grp_keys, maintain_order=True)
            .agg(pl.col("__bl_row__").first().alias("__bl_win__"), pl.len().alias("__bl_n__"))
        )
        n_ties = last_day.filter(pl.col("__bl_n__") > 1).height
        if n_ties and order_by is None:
            rule = f"the last {tie_cols[0]}" if tie_cols else "the last input record"
            warnings.warn(
                f"baseline_flag('{target}'): {n_ties} group(s) have more than one record with a "
                f"result on the last date and time before the reference. The flag goes to "
                f"{rule}; pass order_by= to state the SAP rule.",
                stacklevel=2,
            )

        df = df.with_columns(
            pl.when(pl.col("__bl_row__").is_in(last_day["__bl_win__"]))
            .then(pl.lit("Y"))
            .otherwise(pl.lit(None, dtype=pl.String))
            .alias(target)
        )

        # Clean up temp columns
        drop_cols = ["__bl_row__", "__bl_key__"]
        if ref_df is not None:
            drop_cols.append(_ref_tmp)
        df = df.drop([c for c in drop_cols if c in df.columns])

        new = self._materialise_then(df)

        # Record variable lineage
        from fleet.core.dag import VariableNode as _VN

        var_lineage: list = list(getattr(new, "_lineage", []))
        var_lineage.append(
            _VN(
                name=target,
                verb="baseline_flag",
                sources=[date_col, ref_col, result_col],
                method="fleet.sdtm.namespace.SDTMNamespace.baseline_flag",
                domain=getattr(new, "_domain", None),
            )
        )
        new._lineage = var_lineage

        if ref_df is not None:
            record_pin_lineage(
                new,
                target_col=target,
                verb="baseline_flag",
                source_domain=ref_pin.upper(),
                source_cols=[ref_col],
                join_key=join_key,
                relationship="derivation",
            )
        return new

    # ------------------------------------------------------------------ #
    # 6. condition_add — if condition then value else null                 #
    # ------------------------------------------------------------------ #

    def condition_add(
        self,
        target: str,
        *,
        condition: str | pl.Expr,
        then_value: Any,
    ) -> SDTMNamespace:
        """Set *target* to *then_value* where *condition* is true, else null.

        Args:
            target: Name of the output SDTM variable.
            condition: A Polars expression (or string) that evaluates to a
                boolean mask.
            then_value: The value to assign when *condition* is true.

        Returns:
            SDTMNamespace: The namespace with *target* appended; call
                ``.to_frame()`` to materialise.

        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()
                )
        """
        cond_expr = condition if isinstance(condition, pl.Expr) else parse_expression(condition)
        expr = pl.when(cond_expr).then(pl.lit(then_value)).otherwise(pl.lit(None)).alias(target)
        return self._chain(expr, verb="condition_add")

    # ------------------------------------------------------------------ #
    # 8. ae_aerel — AE causality string mapping                           #
    # ------------------------------------------------------------------ #

    _DEFAULT_AEREL_MAP: dict[str, str] = {
        "yes": "RELATED",
        "y": "RELATED",
        "related": "RELATED",
        "possibly related": "POSSIBLY RELATED",
        "probable": "PROBABLE",
        "no": "NOT RELATED",
        "n": "NOT RELATED",
        "not related": "NOT RELATED",
        "remote": "REMOTE",
        "unlikely related": "UNLIKELY RELATED",
    }

    def ae_aerel(
        self,
        target: str = "AEREL",
        *,
        source: str = "ae_relationship",
        aerel_map: dict[str, str] | None = None,
    ) -> SDTMNamespace:
        """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.

        Args:
            target: Output column name (default ``"AEREL"``).
            source: Raw column containing causality strings.
            aerel_map: Optional ``{raw_text: decoded_value}`` override.

        Returns:
            SDTMNamespace: The namespace with *target* appended; call
                ``.to_frame()`` to materialise.

        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()
                )
        """
        mapping = aerel_map or self._DEFAULT_AEREL_MAP

        raw = pl.col(source).cast(pl.String)
        normalised = raw.str.to_lowercase().str.strip_chars()
        expr = normalised.replace_strict(
            mapping,
            default=raw.str.to_uppercase(),
            return_dtype=pl.String,
        ).alias(target)
        return self._chain(expr, verb="ae_aerel")

    # ------------------------------------------------------------------ #
    # 9. seq — --SEQ sequence number (with SDTM naming convention check)  #
    # ------------------------------------------------------------------ #

    def seq(
        self,
        target: str,
        *,
        order_by: list[str] | None = None,
        by: list[str] | None = None,
        start_at: int = 1,
        check_uniqueness: bool = False,
    ) -> SDTMNamespace:
        """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.

        Args:
            target: Name of the output ``--SEQ`` variable (e.g. ``"AESEQ"``,
                ``"VSSEQ"``).  A warning is emitted if the name does not end
                in ``SEQ``.
            order_by: Columns that define the sort order within each group.
                Records that tie keep their input order, as PROC SORT
                EQUALS.  When ``None``, the current row order is preserved.
            by: Subject-level grouping columns (default
                ``["STUDYID", "USUBJID"]``).  The sequence restarts at
                *start_at* for each unique combination.
            start_at: First sequence value (default ``1``).
            check_uniqueness: If ``True``, emit a warning when the
                combination of *by* + *order_by* does not uniquely
                identify each row.

        Returns:
            SDTMNamespace: A fresh namespace with *target* appended.  Call
                ``.to_frame()`` to materialise.

        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()
                )
        """
        import warnings

        if not target.endswith("SEQ"):
            warnings.warn(
                f"Target variable '{target}' does not end in 'SEQ'. "
                "SDTM --SEQ variables should follow the naming convention.",
                stacklevel=2,
            )

        return super().seq(
            target,
            order_by=order_by,
            by=by,
            start_at=start_at,
            check_uniqueness=check_uniqueness,
        )

    # ------------------------------------------------------------------ #
    # 10. merge is inherited from SharedVerbsMixin (canonical join verb). #
    # ------------------------------------------------------------------ #

    # ------------------------------------------------------------------ #
    # 10. group_by — aggregate records                                     #
    # ------------------------------------------------------------------ #

    def group_by(
        self,
        *,
        by: list[str],
        agg: list[pl.Expr],
    ) -> SDTMNamespace:
        """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.

        Args:
            by: Column(s) to group by.
            agg: List of Polars aggregation expressions.

        Returns:
            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()
                )
        """
        return self._with_lazy(self._lazy.group_by(by, maintain_order=True).agg(agg))

    # ------------------------------------------------------------------ #
    # 13. notsubmitted — mark variable as null / not-submitted            #
    # ------------------------------------------------------------------ #

    def notsubmitted(self, *targets: str) -> SDTMNamespace:
        """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.

        Args:
            *targets: One or more variable names to null out.

        Returns:
            SDTMNamespace: The namespace with each *target* set to null;
                call ``.to_frame()`` to materialise.

        Examples:
            Null out internal scratch fields before submission::

                dm = (
                    raw_df.sdtm
                    .assign("USUBJID", source="subject_id")
                    .notsubmitted("_raw_id", "_site_code")
                    .to_frame()
                )
        """
        exprs = [pl.lit(None).alias(t) for t in targets]
        return self._chain(*exprs, verb="notsubmitted")

    # ------------------------------------------------------------------ #
    # 14. multiple_responses — expand multi-value eCRF fields             #
    # ------------------------------------------------------------------ #

    def multiple_responses(
        self,
        target: str,
        *,
        source: str,
        delimiter: str = "|",
    ) -> SDTMNamespace:
        """Explode a delimited multi-response field into separate rows.

        Splits *source* on *delimiter* and explodes — lazily, as part of
        the accumulated plan.

        Args:
            target: Name of the output column for the split values.
            source: Name of the raw column containing delimited responses.
            delimiter: Separator between responses (default ``"|"``).

        Returns:
            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()
                )
        """
        return self._with_lazy(
            self._lazy.with_columns(pl.col(source).str.split(delimiter).alias("__split__"))
            .explode("__split__")
            .with_columns(pl.col("__split__").str.strip_chars().alias(target))
            .drop("__split__")
        )

    # ------------------------------------------------------------------ #
    # Spec-driven pipeline: from_spec() is inherited from                  #
    # SharedVerbsMixin and executes through fleet.core.spec_registry.      #
    # ------------------------------------------------------------------ #

    # ------------------------------------------------------------------ #
    # Terminal operations                                                  #
    # ------------------------------------------------------------------ #

    def export_supp(
        self,
        *,
        qualifiers: dict[str, dict[str, str]] | None = None,
        spec: str | Path | None = None,
        dataset: str | None = None,
        idvar: str | None = None,
        domain: str | None = None,
    ) -> pl.DataFrame:
        """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):

        1. **qualifiers** dict — explicit metadata per qualifier column.
        2. **spec** + **dataset** — reads ``role: qualifier`` variables
           from a ``define.yaml``.
        3. **Pipeline metadata** — uses ``_nsv_meta`` populated by prior
           ``nsv()`` calls in the chain.

        Args:
            qualifiers: Explicit qualifier metadata mapping.  Keys are
                column names (QNAM), values are dicts with ``"label"``
                and optionally ``"origin"`` and ``"evaluator"`` keys.
            spec: Path to a ``define.yaml`` file.  Used with *dataset*
                to discover ``role: qualifier`` variables.
            dataset: Dataset name within the spec (e.g. ``"DM"``).
            idvar: Name of the identifying variable column (e.g.
                ``"AESEQ"``).  Required for every RDOMAIN except DM;
                for DM, IDVAR and IDVARVAL are null.
            domain: Explicit RDOMAIN value.  When ``None``, inferred
                from the ``DOMAIN`` column in the frame.

        Returns:
            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 ``.0`` end and a date as ISO 8601.

        Raises:
            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"},
                    },
                )
        """
        # Resolve qualifier metadata from one of three sources
        meta: list[NSVMeta] = self._resolve_supp_meta(qualifiers, spec, dataset)

        if not meta:
            if qualifiers is None and spec is None:
                raise DerivationError(
                    "export_supp(): no NSV metadata. Pass qualifiers= or spec=, or call "
                    "export_supp() before to_frame(), which drops the nsv() metadata."
                )
            # qualifiers={} or a spec dataset with no qualifier: a legal empty SUPP
            return pl.DataFrame(schema=dict.fromkeys(_SUPP_COLUMNS, pl.String))

        df = self._lazy.collect()

        if domain:
            rdomain = domain
        elif "DOMAIN" in df.columns and not df.is_empty():
            rdomain = df["DOMAIN"][0]
        else:
            raise DerivationError(
                "export_supp(): RDOMAIN is unknown, because the frame has no DOMAIN value. "
                "Pass domain=."
            )

        absent = [m.target for m in meta if m.target not in df.columns]
        if absent:
            raise DerivationError(
                f"export_supp(): qualifier(s) {absent} are not columns of the frame. "
                f"Available: {df.columns}"
            )
        if idvar is not None and idvar not in df.columns:
            raise DerivationError(f"export_supp(): idvar '{idvar}' is not a column of the frame.")
        if idvar is None and rdomain != "DM":
            raise DerivationError(
                f"export_supp(): give idvar= (for example {rdomain}SEQ) for RDOMAIN "
                f"{rdomain}. Only DM may have no IDVAR (SDTMIG 3.4 section 8.4)."
            )

        qual_cols = [m.target for m in meta]
        label_map = {m.target: m.label for m in meta}
        origin_map = {m.target: m.origin for m in meta}
        eval_map = {m.target: m.evaluator for m in meta}

        base_cols = [c for c in ("STUDYID", "USUBJID") if c in df.columns]
        id_cols = [idvar] if idvar is not None and idvar not in base_cols else []

        # Each qualifier becomes text before the unpivot, so mixed types keep their value.
        supp = df.select(
            *base_cols,
            *id_cols,
            *(to_char(pl.col(c), df.schema[c]).alias(c) for c in qual_cols),
        ).unpivot(
            on=qual_cols,
            index=base_cols + id_cols,
            variable_name="QNAM",
            value_name="QVAL",
        )

        # SDTMIG 3.4: no SUPP record with a null QVAL; a blank is missing too.
        supp = supp.filter(pl.col("QVAL").is_not_null() & (pl.col("QVAL").str.strip_chars() != ""))

        # Add SUPPQUAL metadata columns
        supp = supp.with_columns(
            pl.lit(rdomain).cast(pl.String).alias("RDOMAIN"),
            pl.col("QNAM").replace_strict(label_map, default=None).cast(pl.String).alias("QLABEL"),
            pl.col("QNAM")
            .replace_strict(origin_map, default="Derived")
            .cast(pl.String)
            .alias("QORIG"),
            pl.col("QNAM").replace_strict(eval_map, default=None).cast(pl.String).alias("QEVAL"),
        )

        if idvar is not None:
            supp = supp.with_columns(
                pl.lit(idvar).alias("IDVAR"),
                to_char(pl.col(idvar), df.schema[idvar]).alias("IDVARVAL"),
            )
            if id_cols:
                supp = supp.drop(idvar)
        else:
            supp = supp.with_columns(
                pl.lit(None, dtype=pl.String).alias("IDVAR"),
                pl.lit(None, dtype=pl.String).alias("IDVARVAL"),
            )

        # Ensure STUDYID exists
        if "STUDYID" not in supp.columns:
            supp = supp.with_columns(pl.lit(None, dtype=pl.String).alias("STUDYID"))

        # Ensure USUBJID exists
        if "USUBJID" not in supp.columns:
            supp = supp.with_columns(pl.lit(None, dtype=pl.String).alias("USUBJID"))

        return supp.select(_SUPP_COLUMNS)

    def _resolve_supp_meta(
        self,
        qualifiers: dict[str, dict[str, str]] | None,
        spec: str | Path | None,
        dataset: str | None,
    ) -> list[NSVMeta]:
        """Resolve NSV metadata from qualifiers dict, spec file, or pipeline."""
        # Priority 1: explicit qualifiers dict
        if qualifiers is not None:
            return [
                NSVMeta(
                    target=name,
                    label=info.get("label", ""),
                    origin=info.get("origin", "Derived"),
                    evaluator=info.get("evaluator"),
                )
                for name, info in qualifiers.items()
            ]

        # Priority 2: spec file
        if spec is not None:
            spec_data = load_define_spec(spec)
            if not spec_data.datasets:
                raise DerivationError(f"export_supp(spec='{spec}'): the spec has no datasets.")
            ds = spec_data.select(dataset)
            meta = []
            for var in ds.variables:
                is_qual = (
                    var.role == "qualifier" or (var.derivation or {}).get("algorithm") == "nsv"
                )
                if is_qual:
                    meta.append(
                        NSVMeta(
                            target=var.name,
                            label=var.label or "",
                            origin=var.origin or "Derived",
                            evaluator=var.evaluator,
                        )
                    )
            return meta

        # Priority 3: pipeline metadata
        return list(self._nsv_meta)

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: "dtc", "dt", or "dtm".

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 highest_imputation: "n", "D" or "M", and on a DTM target also "h", "m" and "s". "Y" raises.

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

strptime layout of the raw text (DTC only), e.g. "%m/%d/%Y". Use %y for a two-digit year: it reads 00-69 as 20xx and 70-99 as 19xx.

None

Returns:

Name Type Description
SDTMNamespace SDTMNamespace

The namespace with target appended; call .to_frame() to materialise.

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
def date(
    self,
    target: str,
    *,
    source: str | None = None,
    value: str | None = None,
    output: str | None = None,
    time: str | None = None,
    impute: str = "first",
    impute_time: str | None = None,
    highest: str | None = None,
    flag: bool | str = True,
    min_date: str | None = None,
    max_date: str | None = None,
    format: str | None = None,
) -> SDTMNamespace:
    """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).

    Args:
        target: Name of the output column.
        source: Column name to read values from.  Mutually exclusive
            with *value*.
        value: Literal date value to broadcast.  Mutually exclusive
            with *source*.
        output: Explicit output type: ``"dtc"``, ``"dt"``, or ``"dtm"``.
        time: Time column or literal to combine with date (DTM/DTC).
        impute: Imputation strategy (DT/DTM only).
        impute_time: Time-part imputation override (DTM only).
        highest: Imputation ceiling, as admiral ``highest_imputation``:
            ``"n"``, ``"D"`` or ``"M"``, and on a DTM target also
            ``"h"``, ``"m"`` and ``"s"``.  ``"Y"`` raises.
        flag: DTF/TMF flag control.
        min_date: 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.
        max_date: Upper bound for an imputed date, with the same rule.
        format: ``strptime`` layout of the raw text (DTC only), e.g.
            ``"%m/%d/%Y"``.  Use ``%y`` for a two-digit year: it reads
            00-69 as 20xx and 70-99 as 19xx.

    Returns:
        SDTMNamespace: The namespace with *target* appended; call
            ``.to_frame()`` to materialise.

    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()
            )
    """
    from fleet.adam.utils import (
        _resolve_output_type,
        dtm_source_text,
        impute_and_flag,
        source_text,
    )

    # Validate source / value mutual exclusivity
    if source is not None and value is not None:
        raise DerivationError("Cannot specify both source= and value=. Choose one.")
    if source is None and value is None:
        raise DerivationError("Must specify either source= or value=.")

    output_type = _resolve_output_type(target, output)

    # value= path
    if value is not None:
        if flag is True:
            flag = False
        exprs = impute_and_flag(None, target, value=value, output_type=output_type, flag=flag)
        return self._chain(*exprs, verb="date")

    # source= path
    assert source is not None

    if output_type == "dtc":
        return self._dtc(target, source, time=time, format=format)

    # DT/DTM: delegate to imputation engine
    if time is not None and output_type != "dtm":
        raise DerivationError(
            f"time= parameter is only valid for DTM targets, "
            f"but target '{target}' resolves to output='{output_type}'."
        )
    schema = self._lazy.collect_schema()
    if time is not None:
        src = dtm_source_text(
            schema, source=source, target=target, time=time, input_format=format
        )
    else:
        src = source_text(source, schema.get(source), verb=f"date('{target}')")
    exprs = impute_and_flag(
        src,
        target,
        output_type=output_type,
        impute=impute,
        impute_time=impute_time,
        highest=highest,
        min_date=min_date,
        max_date=max_date,
        flag=flag,
    )
    return self._chain(*exprs, verb="date")

nsv

nsv(
    target,
    *,
    source=None,
    value=None,
    label,
    origin="Derived",
    evaluator=None
)

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 origin: in a define.yaml (Collected, Derived, Assigned, Protocol, Predecessor).

'Derived'
evaluator str | None

Optional evaluator (becomes QEVAL in SUPP).

None

Returns:

Name Type Description
SDTMNamespace SDTMNamespace

The namespace with target appended; call .to_frame() to materialise.

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
def nsv(
    self,
    target: str,
    *,
    source: str | None = None,
    value: Any | None = None,
    label: str,
    origin: str = "Derived",
    evaluator: str | None = None,
) -> SDTMNamespace:
    """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.

    Args:
        target: Name of the qualifier variable (becomes QNAM in SUPP).
        source: Name of the raw column to copy from.  Mutually
            exclusive with *value*.
        value: A literal value to broadcast.  Mutually exclusive with
            *source*.
        label: Human-readable label (becomes QLABEL in SUPP).
        origin: Variable origin (becomes QORIG in SUPP) — the
            Define-XML 2.1 vocabulary shared with ``origin:`` in a
            define.yaml (``Collected``, ``Derived``, ``Assigned``,
            ``Protocol``, ``Predecessor``).
        evaluator: Optional evaluator (becomes QEVAL in SUPP).

    Returns:
        SDTMNamespace: The namespace with *target* appended; call
            ``.to_frame()`` to materialise.

    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()
            )
    """
    if source is None and value is None:
        raise DerivationError("nsv() requires either 'source' or 'value'.")
    if source is not None and value is not None:
        raise DerivationError("nsv() accepts 'source' or 'value', not both.")

    expr = pl.col(source).alias(target) if source else pl.lit(value).alias(target)
    new = self._chain(expr, verb="nsv")
    # Copy-on-write: avoid mutating shared list across copies
    new._nsv_meta = [
        *self._nsv_meta,
        NSVMeta(target=target, label=label, origin=origin, evaluator=evaluator),
    ]
    return new

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. "VSBLFL").

required
date_col str

Column containing the observation date (e.g. "VSDTC").

required
testcd_col str | None

Optional test code column to group by (e.g. "VSTESTCD"). When set, baseline is computed independently for each test.

None
ref_col str

Reference date column in DM (default "RFSTDTC").

'RFSTDTC'
ref_pin str

Pinned domain for the reference date (default "DM").

'DM'
dm DataFrame | None

Optional explicit DataFrame override.

None
by list[str] | None

Additional grouping columns beyond USUBJID and testcd_col, such as --CAT, --SCAT, --SPEC and --METHOD.

None
result_col str | None

Result column (default --STRESC when present, else --ORRES; the prefix comes from target).

None
order_by str | list[str] | None

Column(s) that break a tie on the last date and time (default --SEQ when present).

None

Returns:

Name Type Description
SDTMNamespace SDTMNamespace

The namespace with target appended; call .to_frame() to materialise.

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
def baseline_flag(
    self,
    target: str,
    *,
    date_col: str,
    testcd_col: str | None = None,
    ref_col: str = "RFSTDTC",
    ref_pin: str = "DM",
    dm: pl.DataFrame | None = None,
    by: list[str] | None = None,
    result_col: str | None = None,
    order_by: str | list[str] | None = None,
) -> SDTMNamespace:
    """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.

    Args:
        target: Name of the output flag variable
            (e.g. ``"VSBLFL"``).
        date_col: Column containing the observation date
            (e.g. ``"VSDTC"``).
        testcd_col: Optional test code column to group by
            (e.g. ``"VSTESTCD"``).  When set, baseline is
            computed independently for each test.
        ref_col: Reference date column in DM (default ``"RFSTDTC"``).
        ref_pin: Pinned domain for the reference date
            (default ``"DM"``).
        dm: Optional explicit DataFrame override.
        by: Additional grouping columns beyond USUBJID and
            *testcd_col*, such as ``--CAT``, ``--SCAT``, ``--SPEC`` and
            ``--METHOD``.
        result_col: Result column (default ``--STRESC`` when present,
            else ``--ORRES``; the prefix comes from *target*).
        order_by: Column(s) that break a tie on the last date and time
            (default ``--SEQ`` when present).

    Returns:
        SDTMNamespace: The namespace with *target* appended; call
            ``.to_frame()`` to materialise.

    Raises:
        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()
            )
    """
    import warnings

    from fleet.core.lineage import record_pin_lineage

    df = self._lazy.collect()

    prefix = next(
        (target[: -len(s)] for s in ("LOBXFL", "BLFL") if target.endswith(s) and target != s),
        target[:2],
    )
    if result_col is None:
        candidates = (f"{prefix}STRESC", f"{prefix}ORRES")
        result_col = next((c for c in candidates if c in df.columns), None)
        if result_col is None:
            raise DerivationError(
                f"baseline_flag('{target}'): neither {candidates[0]} nor {candidates[1]} "
                "is in the frame. Name the result column with result_col=."
            )
    elif result_col not in df.columns:
        raise DerivationError(
            f"baseline_flag('{target}'): result_col={result_col!r} is not in the frame."
        )

    # Resolve reference date
    if dm is not None:
        ref_df = dm
        join_key = ["USUBJID"]
    elif ref_col in df.columns:
        ref_df = None
        join_key = []
    else:
        ref_df = self._require_pin(ref_pin.upper(), "baseline_flag", columns=[ref_col])
        join_key = self._pin_join_key(ref_pin.upper())

    # Join reference if needed
    _ref_tmp = f"__pin_{ref_col}__"
    if ref_df is not None:
        from fleet.core.joins import refuse_repeated_keys

        ref_slim = ref_df.select(
            [c for c in dict.fromkeys(join_key + [ref_col]) if c in ref_df.columns]
        )
        if join_key:
            ref_slim = refuse_repeated_keys(
                "baseline_flag()",
                ref_slim,
                join_key,
                source="DM" if dm is not None else ref_pin.upper(),
            )
        ref_slim = ref_slim.rename({ref_col: _ref_tmp})
        df = df.join(ref_slim, on=join_key, how="left", nulls_equal=True)
        ref_col_name = _ref_tmp
    else:
        ref_col_name = ref_col

    # Build grouping keys
    grp_keys = ["USUBJID"]
    if testcd_col is not None:
        grp_keys.append(testcd_col)
    if by:
        grp_keys.extend(by)
    grp_keys = list(dict.fromkeys(grp_keys))

    # Last record strictly before the reference (Q1 B, 2026-11-02): on the reference
    # date a record is eligible only when both times are known and it is earlier.
    def day_and_time(name: str) -> tuple[pl.Expr, pl.Expr]:
        dtype = df.schema[name]
        if dtype == pl.String:
            return date_part(pl.col(name)), datetime_part(pl.col(name))
        if isinstance(dtype, pl.Datetime):
            return pl.col(name).cast(pl.Date), pl.col(name).cast(pl.Datetime("us"))
        return pl.col(name), pl.lit(None, dtype=pl.Datetime("us"))

    day, time = day_and_time(date_col)
    ref_day, ref_time = day_and_time(ref_col_name)
    before_ref = (day < ref_day) | ((day == ref_day) & (time < ref_time))

    has_result = pl.col(result_col).is_not_null()
    if df.schema[result_col] == pl.String:
        has_result = has_result & (pl.col(result_col).str.strip_chars() != "")
    stat_col, orres_col, drvfl_col = f"{prefix}STAT", f"{prefix}ORRES", f"{prefix}DRVFL"
    if stat_col in df.columns:
        has_result = has_result & (pl.col(stat_col).fill_null("") != "NOT DONE")
    if orres_col in df.columns and orres_col != result_col:
        orres_ok = pl.col(orres_col).cast(pl.String).str.strip_chars().fill_null("") != ""
        if drvfl_col in df.columns:
            orres_ok = orres_ok | (pl.col(drvfl_col).fill_null("") == "Y")
        has_result = has_result & orres_ok

    eligible = before_ref.fill_null(False) & has_result.fill_null(False)
    key = pl.coalesce(time, day.cast(pl.Datetime("us")))
    df = df.with_row_index("__bl_row__").with_columns(
        pl.when(eligible).then(key).alias("__bl_key__"),
    )

    if order_by is not None:
        tie_cols = [order_by] if isinstance(order_by, str) else list(order_by)
    else:
        tie_cols = [c for c in (f"{prefix}SEQ",) if c in df.columns]
    last_day = (
        df.filter(pl.col("__bl_key__").is_not_null())
        .filter(pl.col("__bl_key__") == pl.col("__bl_key__").max().over(grp_keys))
        .sort(["__bl_key__", *tie_cols, "__bl_row__"], descending=True, nulls_last=True)
        .group_by(grp_keys, maintain_order=True)
        .agg(pl.col("__bl_row__").first().alias("__bl_win__"), pl.len().alias("__bl_n__"))
    )
    n_ties = last_day.filter(pl.col("__bl_n__") > 1).height
    if n_ties and order_by is None:
        rule = f"the last {tie_cols[0]}" if tie_cols else "the last input record"
        warnings.warn(
            f"baseline_flag('{target}'): {n_ties} group(s) have more than one record with a "
            f"result on the last date and time before the reference. The flag goes to "
            f"{rule}; pass order_by= to state the SAP rule.",
            stacklevel=2,
        )

    df = df.with_columns(
        pl.when(pl.col("__bl_row__").is_in(last_day["__bl_win__"]))
        .then(pl.lit("Y"))
        .otherwise(pl.lit(None, dtype=pl.String))
        .alias(target)
    )

    # Clean up temp columns
    drop_cols = ["__bl_row__", "__bl_key__"]
    if ref_df is not None:
        drop_cols.append(_ref_tmp)
    df = df.drop([c for c in drop_cols if c in df.columns])

    new = self._materialise_then(df)

    # Record variable lineage
    from fleet.core.dag import VariableNode as _VN

    var_lineage: list = list(getattr(new, "_lineage", []))
    var_lineage.append(
        _VN(
            name=target,
            verb="baseline_flag",
            sources=[date_col, ref_col, result_col],
            method="fleet.sdtm.namespace.SDTMNamespace.baseline_flag",
            domain=getattr(new, "_domain", None),
        )
    )
    new._lineage = var_lineage

    if ref_df is not None:
        record_pin_lineage(
            new,
            target_col=target,
            verb="baseline_flag",
            source_domain=ref_pin.upper(),
            source_cols=[ref_col],
            join_key=join_key,
            relationship="derivation",
        )
    return new

condition_add

condition_add(target, *, condition, then_value)

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 .to_frame() to materialise.

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
def condition_add(
    self,
    target: str,
    *,
    condition: str | pl.Expr,
    then_value: Any,
) -> SDTMNamespace:
    """Set *target* to *then_value* where *condition* is true, else null.

    Args:
        target: Name of the output SDTM variable.
        condition: A Polars expression (or string) that evaluates to a
            boolean mask.
        then_value: The value to assign when *condition* is true.

    Returns:
        SDTMNamespace: The namespace with *target* appended; call
            ``.to_frame()`` to materialise.

    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()
            )
    """
    cond_expr = condition if isinstance(condition, pl.Expr) else parse_expression(condition)
    expr = pl.when(cond_expr).then(pl.lit(then_value)).otherwise(pl.lit(None)).alias(target)
    return self._chain(expr, verb="condition_add")

ae_aerel

ae_aerel(
    target="AEREL",
    *,
    source="ae_relationship",
    aerel_map=None
)

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").

'AEREL'
source str

Raw column containing causality strings.

'ae_relationship'
aerel_map dict[str, str] | None

Optional {raw_text: decoded_value} override.

None

Returns:

Name Type Description
SDTMNamespace SDTMNamespace

The namespace with target appended; call .to_frame() to materialise.

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
def ae_aerel(
    self,
    target: str = "AEREL",
    *,
    source: str = "ae_relationship",
    aerel_map: dict[str, str] | None = None,
) -> SDTMNamespace:
    """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.

    Args:
        target: Output column name (default ``"AEREL"``).
        source: Raw column containing causality strings.
        aerel_map: Optional ``{raw_text: decoded_value}`` override.

    Returns:
        SDTMNamespace: The namespace with *target* appended; call
            ``.to_frame()`` to materialise.

    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()
            )
    """
    mapping = aerel_map or self._DEFAULT_AEREL_MAP

    raw = pl.col(source).cast(pl.String)
    normalised = raw.str.to_lowercase().str.strip_chars()
    expr = normalised.replace_strict(
        mapping,
        default=raw.str.to_uppercase(),
        return_dtype=pl.String,
    ).alias(target)
    return self._chain(expr, verb="ae_aerel")

seq

seq(
    target,
    *,
    order_by=None,
    by=None,
    start_at=1,
    check_uniqueness=False
)

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 --SEQ variable (e.g. "AESEQ", "VSSEQ"). A warning is emitted if the name does not end in SEQ.

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, the current row order is preserved.

None
by list[str] | None

Subject-level grouping columns (default ["STUDYID", "USUBJID"]). The sequence restarts at start_at for each unique combination.

None
start_at int

First sequence value (default 1).

1
check_uniqueness bool

If True, emit a warning when the combination of by + order_by does not uniquely identify each row.

False

Returns:

Name Type Description
SDTMNamespace SDTMNamespace

A fresh namespace with target appended. Call .to_frame() to materialise.

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
def seq(
    self,
    target: str,
    *,
    order_by: list[str] | None = None,
    by: list[str] | None = None,
    start_at: int = 1,
    check_uniqueness: bool = False,
) -> SDTMNamespace:
    """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.

    Args:
        target: Name of the output ``--SEQ`` variable (e.g. ``"AESEQ"``,
            ``"VSSEQ"``).  A warning is emitted if the name does not end
            in ``SEQ``.
        order_by: Columns that define the sort order within each group.
            Records that tie keep their input order, as PROC SORT
            EQUALS.  When ``None``, the current row order is preserved.
        by: Subject-level grouping columns (default
            ``["STUDYID", "USUBJID"]``).  The sequence restarts at
            *start_at* for each unique combination.
        start_at: First sequence value (default ``1``).
        check_uniqueness: If ``True``, emit a warning when the
            combination of *by* + *order_by* does not uniquely
            identify each row.

    Returns:
        SDTMNamespace: A fresh namespace with *target* appended.  Call
            ``.to_frame()`` to materialise.

    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()
            )
    """
    import warnings

    if not target.endswith("SEQ"):
        warnings.warn(
            f"Target variable '{target}' does not end in 'SEQ'. "
            "SDTM --SEQ variables should follow the naming convention.",
            stacklevel=2,
        )

    return super().seq(
        target,
        order_by=order_by,
        by=by,
        start_at=start_at,
        check_uniqueness=check_uniqueness,
    )

group_by

group_by(*, by, agg)

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
def group_by(
    self,
    *,
    by: list[str],
    agg: list[pl.Expr],
) -> SDTMNamespace:
    """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.

    Args:
        by: Column(s) to group by.
        agg: List of Polars aggregation expressions.

    Returns:
        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()
            )
    """
    return self._with_lazy(self._lazy.group_by(by, maintain_order=True).agg(agg))

notsubmitted

notsubmitted(*targets)

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 .to_frame() to materialise.

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
def notsubmitted(self, *targets: str) -> SDTMNamespace:
    """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.

    Args:
        *targets: One or more variable names to null out.

    Returns:
        SDTMNamespace: The namespace with each *target* set to null;
            call ``.to_frame()`` to materialise.

    Examples:
        Null out internal scratch fields before submission::

            dm = (
                raw_df.sdtm
                .assign("USUBJID", source="subject_id")
                .notsubmitted("_raw_id", "_site_code")
                .to_frame()
            )
    """
    exprs = [pl.lit(None).alias(t) for t in targets]
    return self._chain(*exprs, verb="notsubmitted")

multiple_responses

multiple_responses(target, *, source, delimiter='|')

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
def multiple_responses(
    self,
    target: str,
    *,
    source: str,
    delimiter: str = "|",
) -> SDTMNamespace:
    """Explode a delimited multi-response field into separate rows.

    Splits *source* on *delimiter* and explodes — lazily, as part of
    the accumulated plan.

    Args:
        target: Name of the output column for the split values.
        source: Name of the raw column containing delimited responses.
        delimiter: Separator between responses (default ``"|"``).

    Returns:
        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()
            )
    """
    return self._with_lazy(
        self._lazy.with_columns(pl.col(source).str.split(delimiter).alias("__split__"))
        .explode("__split__")
        .with_columns(pl.col("__split__").str.strip_chars().alias(target))
        .drop("__split__")
    )

export_supp

export_supp(
    *,
    qualifiers=None,
    spec=None,
    dataset=None,
    idvar=None,
    domain=None
)

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):

  1. qualifiers dict — explicit metadata per qualifier column.
  2. spec + dataset — reads role: qualifier variables from a define.yaml.
  3. Pipeline metadata — uses _nsv_meta populated by prior nsv() 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 "label" and optionally "origin" and "evaluator" keys.

None
spec str | Path | None

Path to a define.yaml file. Used with dataset to discover role: qualifier variables.

None
dataset str | None

Dataset name within the spec (e.g. "DM").

None
idvar str | None

Name of the identifying variable column (e.g. "AESEQ"). Required for every RDOMAIN except DM; for DM, IDVAR and IDVARVAL are null.

None
domain str | None

Explicit RDOMAIN value. When None, inferred from the DOMAIN column in the frame.

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 .0 end and a date as ISO 8601.

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
def export_supp(
    self,
    *,
    qualifiers: dict[str, dict[str, str]] | None = None,
    spec: str | Path | None = None,
    dataset: str | None = None,
    idvar: str | None = None,
    domain: str | None = None,
) -> pl.DataFrame:
    """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):

    1. **qualifiers** dict — explicit metadata per qualifier column.
    2. **spec** + **dataset** — reads ``role: qualifier`` variables
       from a ``define.yaml``.
    3. **Pipeline metadata** — uses ``_nsv_meta`` populated by prior
       ``nsv()`` calls in the chain.

    Args:
        qualifiers: Explicit qualifier metadata mapping.  Keys are
            column names (QNAM), values are dicts with ``"label"``
            and optionally ``"origin"`` and ``"evaluator"`` keys.
        spec: Path to a ``define.yaml`` file.  Used with *dataset*
            to discover ``role: qualifier`` variables.
        dataset: Dataset name within the spec (e.g. ``"DM"``).
        idvar: Name of the identifying variable column (e.g.
            ``"AESEQ"``).  Required for every RDOMAIN except DM;
            for DM, IDVAR and IDVARVAL are null.
        domain: Explicit RDOMAIN value.  When ``None``, inferred
            from the ``DOMAIN`` column in the frame.

    Returns:
        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 ``.0`` end and a date as ISO 8601.

    Raises:
        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"},
                },
            )
    """
    # Resolve qualifier metadata from one of three sources
    meta: list[NSVMeta] = self._resolve_supp_meta(qualifiers, spec, dataset)

    if not meta:
        if qualifiers is None and spec is None:
            raise DerivationError(
                "export_supp(): no NSV metadata. Pass qualifiers= or spec=, or call "
                "export_supp() before to_frame(), which drops the nsv() metadata."
            )
        # qualifiers={} or a spec dataset with no qualifier: a legal empty SUPP
        return pl.DataFrame(schema=dict.fromkeys(_SUPP_COLUMNS, pl.String))

    df = self._lazy.collect()

    if domain:
        rdomain = domain
    elif "DOMAIN" in df.columns and not df.is_empty():
        rdomain = df["DOMAIN"][0]
    else:
        raise DerivationError(
            "export_supp(): RDOMAIN is unknown, because the frame has no DOMAIN value. "
            "Pass domain=."
        )

    absent = [m.target for m in meta if m.target not in df.columns]
    if absent:
        raise DerivationError(
            f"export_supp(): qualifier(s) {absent} are not columns of the frame. "
            f"Available: {df.columns}"
        )
    if idvar is not None and idvar not in df.columns:
        raise DerivationError(f"export_supp(): idvar '{idvar}' is not a column of the frame.")
    if idvar is None and rdomain != "DM":
        raise DerivationError(
            f"export_supp(): give idvar= (for example {rdomain}SEQ) for RDOMAIN "
            f"{rdomain}. Only DM may have no IDVAR (SDTMIG 3.4 section 8.4)."
        )

    qual_cols = [m.target for m in meta]
    label_map = {m.target: m.label for m in meta}
    origin_map = {m.target: m.origin for m in meta}
    eval_map = {m.target: m.evaluator for m in meta}

    base_cols = [c for c in ("STUDYID", "USUBJID") if c in df.columns]
    id_cols = [idvar] if idvar is not None and idvar not in base_cols else []

    # Each qualifier becomes text before the unpivot, so mixed types keep their value.
    supp = df.select(
        *base_cols,
        *id_cols,
        *(to_char(pl.col(c), df.schema[c]).alias(c) for c in qual_cols),
    ).unpivot(
        on=qual_cols,
        index=base_cols + id_cols,
        variable_name="QNAM",
        value_name="QVAL",
    )

    # SDTMIG 3.4: no SUPP record with a null QVAL; a blank is missing too.
    supp = supp.filter(pl.col("QVAL").is_not_null() & (pl.col("QVAL").str.strip_chars() != ""))

    # Add SUPPQUAL metadata columns
    supp = supp.with_columns(
        pl.lit(rdomain).cast(pl.String).alias("RDOMAIN"),
        pl.col("QNAM").replace_strict(label_map, default=None).cast(pl.String).alias("QLABEL"),
        pl.col("QNAM")
        .replace_strict(origin_map, default="Derived")
        .cast(pl.String)
        .alias("QORIG"),
        pl.col("QNAM").replace_strict(eval_map, default=None).cast(pl.String).alias("QEVAL"),
    )

    if idvar is not None:
        supp = supp.with_columns(
            pl.lit(idvar).alias("IDVAR"),
            to_char(pl.col(idvar), df.schema[idvar]).alias("IDVARVAL"),
        )
        if id_cols:
            supp = supp.drop(idvar)
    else:
        supp = supp.with_columns(
            pl.lit(None, dtype=pl.String).alias("IDVAR"),
            pl.lit(None, dtype=pl.String).alias("IDVARVAL"),
        )

    # Ensure STUDYID exists
    if "STUDYID" not in supp.columns:
        supp = supp.with_columns(pl.lit(None, dtype=pl.String).alias("STUDYID"))

    # Ensure USUBJID exists
    if "USUBJID" not in supp.columns:
        supp = supp.with_columns(pl.lit(None, dtype=pl.String).alias("USUBJID"))

    return supp.select(_SUPP_COLUMNS)

options: show_root_heading: true heading_level: 2 members_order: source inherited_members: true filters: ["!^_"]