{"record":{"id":"80d1a4f2b59bc8ec","repo":"apache/beam","slug":"r-has-greater-than-microsecond-precision-converting-it-to","errorCode":null,"errorMessage":"%r has greater than microsecond precision, converting it to micros may lose precision. Use to_precision(6, allow_lossy_conversion=True) to explicitly truncate it first, or use nanos instead.","messagePattern":"%r has greater than microsecond precision, converting it to micros may lose precision\\. Use to_precision\\(6, allow_lossy_conversion=True\\) to explicitly truncate it first, or use nanos instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/utils/timestamp.py","lineNumber":232,"sourceCode":"    \"\"\"Returns the timestamp in seconds.\"\"\"\n    return self._seconds\n\n  def subseconds(self) -> int:\n    \"\"\"Returns the fraction of a second, in units of 10**-precision seconds.\n\n    Always non-negative and less than 10**precision\n    \"\"\"\n    return self._subseconds\n\n  def precision(self) -> int:\n    \"\"\"Returns the precision of this Timestamp.\"\"\"\n    return self._precision\n\n  @property\n  def micros(self) -> int:\n    \"\"\"Returns the total number of microseconds since the epoch.\"\"\"\n    if self._precision > Timestamp.MICROS_PRECISION:\n      raise ValueError(\n          '%r has greater than microsecond precision, converting it to '\n          'micros may lose precision. Use to_precision(6, '\n          'allow_lossy_conversion=True) to explicitly truncate it first, '\n          'or use nanos instead.' % self)\n    return self._total(Timestamp.MICROS_PRECISION)\n\n  @property\n  def nanos(self) -> int:\n    \"\"\"Returns the total number of nanoseconds since the epoch.\"\"\"\n    return self._total(Timestamp.NANOS_PRECISION)\n\n  def to_precision(\n      self,\n      precision: int,\n      allow_lossy_conversion: bool = False) -> 'Timestamp':\n    \"\"\"Returns this Timestamp converted to the given precision.\n\n    Increasing precision is always lossless. Decreasing precision raises","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/utils/timestamp.py#L214-L250","documentation":"apache_beam.utils.timestamp.Timestamp.micros raises ValueError when the timestamp was created with sub-microsecond (nanosecond) precision and the caller asks for the microsecond value. Reading `.micros` would silently truncate the extra digits, so the library forces you to either explicitly truncate with to_precision(6, allow_lossy_conversion=True) or read `.nanos` instead.","triggerScenarios":"Accessing the `micros` property on any Timestamp whose _precision exceeds Timestamp.MICROS_PRECISION (6), e.g. a nanosecond-precision Timestamp from Timestamp.of, arithmetic, or to_precision(9).","commonSituations":"Processing event timestamps coming from sources with nanosecond resolution (e.g. fuzzer/DoFn tests, protobuf Timestamp with non-zero nanos, data from formats like Spanner or high-resolution clocks), then reading `.micros` in a DoFn or test assertion.","solutions":["Read `.nanos` instead of `.micros` if you need the full precision value.","Call ts.to_precision(6, allow_lossy_conversion=True).micros to explicitly truncate to microseconds.","Ensure upstream timestamps are constructed at microsecond precision if micros is the intended unit."],"exampleFix":"// before\nmicros = ts.micros  # raises for nanosecond-precision ts\n// after\nmicros = ts.to_precision(6, allow_lossy_conversion=True).micros\n# or, without losing precision:\nexact = ts.nanos","handlingStrategy":"validation","validationCode":"def safe_micros(ts):\n    if ts.precision > 6:\n        ts = ts.to_precision(6, allow_lossy_conversion=True)\n    return ts.micros","typeGuard":"def has_micro_precision(ts):\n    return ts.precision <= 6","tryCatchPattern":"try:\n    micros = ts.micros\nexcept ValueError:\n    micros = ts.nanos // 1000  # explicit truncation","preventionTips":["Prefer `.nanos` when handling timestamps of unknown precision.","Normalize timestamps to microsecond precision at pipeline ingest boundaries.","Check `ts.precision` before accessing unit-specific properties."],"tags":["python","apache-beam","timestamp","precision-loss"],"backgroundTag":"value-out-of-range","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}