pathwaycom/pathway · error · ValueError

unit has to be one of s, ms, us, ns but is {unit}.

Error message

unit has to be one of s, ms, us, ns but is {unit}.

What it means

The dt.timestamp() method converts a datetime column to a numeric UNIX timestamp and requires the unit to be exactly one of 's', 'ms', 'us', 'ns' so it knows the scale to divide by. Any other string (or a unit supported by pandas but not listed here, like 'M' or 'm') raises this ValueError before an expression is built.

Source

Thrown at python/pathway/internals/expressions/date_time.py:478

            return expr.MethodCallExpression(
                (
                    (
                        dt.DATE_TIME_NAIVE,
                        dt.INT,
                        api.Expression.date_time_naive_timestamp_ns,
                    ),
                    (
                        dt.DATE_TIME_UTC,
                        dt.INT,
                        api.Expression.date_time_utc_timestamp_ns,
                    ),
                ),
                "dt.timestamp",
                self._expression,
            )
        else:
            if unit not in ("s", "ms", "us", "ns"):
                raise ValueError(f"unit has to be one of s, ms, us, ns but is {unit}.")
            return expr.MethodCallExpression(
                (
                    (
                        (dt.DATE_TIME_NAIVE, dt.STR),
                        dt.FLOAT,
                        api.Expression.date_time_naive_timestamp,
                    ),
                    (
                        (dt.DATE_TIME_UTC, dt.STR),
                        dt.FLOAT,
                        api.Expression.date_time_utc_timestamp,
                    ),
                ),
                "dt.timestamp",
                self._expression,
                unit,
            )

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Use one of the exact lowercase strings 's', 'ms', 'us', 'ns'
  2. If the unit comes from config, normalize it: unit.strip().lower() and map synonyms (seconds->s, milliseconds->ms) before the call
  3. Pre-validate against the tuple ('s','ms','us','ns') and fail fast with your own clearer error

Example fix

// before
value = pw.this.when.dt.timestamp(unit='seconds')
// after
value = pw.this.when.dt.timestamp(unit='s')
Defensive patterns

Strategy: validation

Validate before calling

VALID_UNITS = ("s", "ms", "us", "ns")

def normalize_unit(u: str) -> str:
    u = u.strip().lower()
    aliases = {'seconds': 's', 'sec': 's', 'milliseconds': 'ms', 'millis': 'ms', 'microseconds': 'us', 'nanoseconds': 'ns'}
    u = aliases.get(u, u)
    assert u in VALID_UNITS, f"unit must be one of {VALID_UNITS}, got {u!r}"
    return u

Type guard

def is_timestamp_unit(u) -> bool:
    return isinstance(u, str) and u in ("s", "ms", "us", "ns")

Prevention

When it happens

Trigger: Calling pw.this.ts.dt.timestamp(unit='m') ('m' means minute in some APIs, not allowed here); passing unit='microseconds'/'seconds' spelled out; passing None or leaving a variable uninitialized; unit from user config with wrong casing ('S').

Common situations: Porting code from pd.Timestamp.timestamp or from pandas' to_datetime(unit=...) whose accepted set differs; config strings copy-pasted from other tooling; assuming case-insensitivity.

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/698f233dc9caaf23. Report an issue: GitHub.