Skip to content

eds_scikit.utils.datetime_helpers

add_timedelta

add_timedelta(series: Series, **kwargs) -> Series

Adds a unique timedelta to a Pandas or Koalas Series

Source code in eds_scikit/utils/datetime_helpers.py
 9
10
11
12
13
def add_timedelta(series: Series, **kwargs) -> Series:
    """
    Adds a unique timedelta to a Pandas or Koalas Series
    """
    return series.map(lambda d: d + timedelta(**kwargs))

substract_datetime

substract_datetime(series_1: Series, series_2: Series, out: str = 'seconds') -> Series

Substract 2 datetime series and return the number of seconds or hours between them.

Source code in eds_scikit/utils/datetime_helpers.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def substract_datetime(
    series_1: Series,
    series_2: Series,
    out: str = "seconds",
) -> Series:
    """
    Substract 2 datetime series and return the number of seconds or hours
    between them.
    """

    if out not in ["seconds", "hours"]:
        raise ValueError("the 'out' parameter should be in ['hours','seconds']")
    if not (
        np.issubdtype(series_1.dtype, np.datetime64)
        and np.issubdtype(series_2.dtype, np.datetime64)
    ):
        raise TypeError("One of the provided Serie isn't a datetime Serie")

    if is_pandas(series_1) and is_pandas(series_2):
        diff = (series_1 - series_2).dt.total_seconds()

    elif is_koalas(series_1) and is_koalas(series_2):
        diff = series_1 - series_2

    else:
        raise TypeError("Both series should either be a Koalas or Pandas Serie")

    if out == "hours":
        return diff / 3600
    return diff
Back to top