Skip to content

archaeo_super_prompt.dataset.normalization.intervention_date.period_to_dd

[docs] module archaeo_super_prompt.dataset.normalization.intervention_date.period_to_dd

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Fix functions for transforming a period of intervention into a start date and a duration."""

from .utils import Duration
import pandas as pd
from datetime import timedelta


def fix_start_and_duration(df: pd.DataFrame):
    """Fix the intervention date and duration when it is sure that the whole intervention period has been written."""
    df = df[~(df["end_date"] - df["start_date"] < timedelta(0))]
    delta = df["end_date"] - df["start_date"]
    df = df.assign(
        norm_duration=delta.apply(
            lambda d: Duration(d.days + 1, "day")
            if isinstance(d, timedelta)
            else None
        ),
    ).where(
        (delta > timedelta(0))
        & (df["precision"] == "day")
        & (df["norm_duration"].isnull()),
        df,
    )
    return df.assign(
        end_date=df["start_date"],
    ).where((delta > timedelta(0)) & (df["precision"] == "day"), df)