Skip to content

Preprocessing

cibmangotree.preprocessing

Modules:

Name Description
series_semantic

Modules

series_semantic

Functions:

Name Description
constant_series

Create a series with a constant value for each row of series.

parse_datetime_with_tz

Parse datetime strings with timezone info (both abbreviations and offsets)

parse_time_military

Parse time strings with multiple format attempts

Attributes
Functions:
constant_series(series, constant)

Create a series with a constant value for each row of series.

Source code in src/cibmangotree/preprocessing/series_semantic.py
255
256
257
def constant_series(series: pl.Series, constant) -> pl.Series:
    """Create a series with a constant value for each row of `series`."""
    return pl.Series([constant] * series.len(), dtype=pl.Boolean)
parse_datetime_with_tz(s)

Parse datetime strings with timezone info (both abbreviations and offsets)

Source code in src/cibmangotree/preprocessing/series_semantic.py
57
58
59
60
61
62
63
64
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
def parse_datetime_with_tz(s: pl.Series) -> pl.Series:
    """Parse datetime strings with timezone info (both abbreviations and offsets)"""
    import warnings

    # Handle timezone abbreviations like "UTC", "EST"
    tz_abbrev_regex = r" ([A-Z]{3,4})$"  # UTC, EST, etc.

    # Handle timezone offsets like "-05:00", "+00:00", and compact "+0000"
    tz_offset_regex = r"[+-]\d{2}:?\d{2}$"

    # Handle ISO 8601 "Z" suffix (UTC)
    z_suffix_regex = r"Z$"

    stripped = (
        s.str.replace(tz_abbrev_regex, "")
        .str.replace(tz_offset_regex, "")
        .str.replace(z_suffix_regex, "")
    )
    parsed = stripped.str.strptime(pl.Datetime(), strict=False)

    # Only warn about mixed timezones among values that actually parsed as datetimes
    ok = parsed.is_not_null()
    if ok.any():
        candidates = s.filter(ok)
        unique_tz = set()
        for regex in (tz_abbrev_regex, tz_offset_regex, z_suffix_regex):
            for match_list in candidates.str.extract_all(regex).to_list():
                if match_list:
                    unique_tz.update(match_list)
        if len(unique_tz) > 1:
            col = f" '{s.name}'" if s.name else ""
            warnings.warn(
                f"Multiple timezones found in datetime column{col}: {list(unique_tz)}. "
                f"Assuming all timestamps represent the same timezone for analysis purposes.",
                UserWarning,
            )

    return parsed
parse_time_military(s)

Parse time strings with multiple format attempts

Source code in src/cibmangotree/preprocessing/series_semantic.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def parse_time_military(s: pl.Series) -> pl.Series:
    """Parse time strings with multiple format attempts"""
    # Try different time formats
    FORMATS_TO_TRY = ["%H:%M:%S", "%H:%M", "%I:%M:%S %p", "%I:%M %p"]

    for fmt in FORMATS_TO_TRY:
        try:
            result = s.str.strptime(pl.Time, format=fmt, strict=False)
            if result.is_not_null().sum() > 0:  # If any parsed successfully
                return result
        except:
            continue

    # If all formats fail, return nulls
    return pl.Series([None] * s.len(), dtype=pl.Time)