Skip to content

ExprDateTimeNameSpace.offset_by

Functionally same as polars.Expr.dt.offset_by. However arguments are different.

Parameters:

Name Type Description Default
num int or float

number of units to offset by

required
unit str

unit of offset. One of "ms", "s", "m", "h", "d", "M", "y"

required
Source code in pyquokka/expression.py
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
def offset_by(self, num, unit):

    """
    Functionally same as polars.Expr.dt.offset_by. However arguments are different.

    Args:
        num (int or float): number of units to offset by
        unit (str): unit of offset. One of "ms", "s", "m", "h", "d", "M", "y"
    """

    assert type(unit) == str and unit in {"ms", "s", "m", "h", "d",  "M", "y"}, "unit must be one of 'ms', 's', 'm', 'h', 'd', 'M', 'y'"
    mapping = {"ms": "millisecond", "s": "second", "m": "minute", "h": "hour", "d": "day", "M": "month", "y": "year"}
    if type(num) == int or type(num) == float:
        return Expression(self.expr.sqlglot_expr + sqlglot.parse_one("interval {} {}".format(num, mapping[unit])))
    else:
        raise Exception("num must be an int or float. Offseting by a column is not supported yet")