{"record":{"id":"698f233dc9caaf23","repo":"pathwaycom/pathway","slug":"unit-has-to-be-one-of-s-ms-us-ns-but-is-unit","errorCode":null,"errorMessage":"unit has to be one of s, ms, us, ns but is {unit}.","messagePattern":"unit has to be one of s, ms, us, ns but is (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/expressions/date_time.py","lineNumber":478,"sourceCode":"            return expr.MethodCallExpression(\n                (\n                    (\n                        dt.DATE_TIME_NAIVE,\n                        dt.INT,\n                        api.Expression.date_time_naive_timestamp_ns,\n                    ),\n                    (\n                        dt.DATE_TIME_UTC,\n                        dt.INT,\n                        api.Expression.date_time_utc_timestamp_ns,\n                    ),\n                ),\n                \"dt.timestamp\",\n                self._expression,\n            )\n        else:\n            if unit not in (\"s\", \"ms\", \"us\", \"ns\"):\n                raise ValueError(f\"unit has to be one of s, ms, us, ns but is {unit}.\")\n            return expr.MethodCallExpression(\n                (\n                    (\n                        (dt.DATE_TIME_NAIVE, dt.STR),\n                        dt.FLOAT,\n                        api.Expression.date_time_naive_timestamp,\n                    ),\n                    (\n                        (dt.DATE_TIME_UTC, dt.STR),\n                        dt.FLOAT,\n                        api.Expression.date_time_utc_timestamp,\n                    ),\n                ),\n                \"dt.timestamp\",\n                self._expression,\n                unit,\n            )\n","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/expressions/date_time.py#L460-L496","documentation":"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.","triggerScenarios":"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').","commonSituations":"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.","solutions":["Use one of the exact lowercase strings 's', 'ms', 'us', 'ns'","If the unit comes from config, normalize it: unit.strip().lower() and map synonyms (seconds->s, milliseconds->ms) before the call","Pre-validate against the tuple ('s','ms','us','ns') and fail fast with your own clearer error"],"exampleFix":"// before\nvalue = pw.this.when.dt.timestamp(unit='seconds')\n// after\nvalue = pw.this.when.dt.timestamp(unit='s')","handlingStrategy":"validation","validationCode":"VALID_UNITS = (\"s\", \"ms\", \"us\", \"ns\")\n\ndef normalize_unit(u: str) -> str:\n    u = u.strip().lower()\n    aliases = {'seconds': 's', 'sec': 's', 'milliseconds': 'ms', 'millis': 'ms', 'microseconds': 'us', 'nanoseconds': 'ns'}\n    u = aliases.get(u, u)\n    assert u in VALID_UNITS, f\"unit must be one of {VALID_UNITS}, got {u!r}\"\n    return u","typeGuard":"def is_timestamp_unit(u) -> bool:\n    return isinstance(u, str) and u in (\"s\", \"ms\", \"us\", \"ns\")","tryCatchPattern":null,"preventionTips":["Pass only lowercase 's'/'ms'/'us'/'ns'","Map long unit names from config to short ones once, at the boundary","Unit-test the config values your pipeline reads"],"tags":["pathway","datetime","timestamp","unit-validation","api-misuse"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}