apache/beam · error · TypeError

Cannot interpret %s %s as seconds.

Error message

Cannot interpret %s %s as seconds.

What it means

Timestamp.__init__ validates that the seconds argument is an int or float before converting to microseconds; anything else (str, Decimal, datetime, None) raises this TypeError. Beam's Timestamp intentionally does not coerce arbitrary types to avoid silent precision/semantic bugs.

Source

Thrown at sdks/python/apache_beam/utils/timestamp.py:83

  nanos). Defaults to microseconds.
  If ``seconds`` is a float, the fractional part will be captured up
  to ``precision`` digits.

  Lossy conversion operations will throw an error unless
  ``allow_lossy_conversion=True`` is specified (e.g. see ``to_utc_datetime``).
  """
  MICROS_PRECISION = 6
  NANOS_PRECISION = 9

  def __init__(
      self,
      seconds: Union[int, float] = 0,
      subseconds: Union[int, float] = 0,
      precision: int = MICROS_PRECISION,
      *,
      micros: Optional[Union[int, float]] = None) -> None:
    if not isinstance(seconds, (int, float)):
      raise TypeError(
          'Cannot interpret %s %s as seconds.' % (seconds, type(seconds)))
    if not isinstance(subseconds, (int, float)):
      raise TypeError(
          'Cannot interpret %s %s as subseconds.' %
          (subseconds, type(subseconds)))
    if not isinstance(precision, int):
      raise TypeError(
          'Cannot interpret %s %s as precision.' % (precision, type(precision)))
    if not 0 <= precision <= Timestamp.NANOS_PRECISION:
      raise ValueError(
          'Timestamp precision must be between 0 and %d (inclusive), '
          'but was %d.' % (Timestamp.NANOS_PRECISION, precision))
    if micros is not None:
      if not isinstance(micros, (int, float)):
        raise TypeError(
            'Cannot interpret %s %s as micros.' % (micros, type(micros)))
      if subseconds:
        raise ValueError(

View on GitHub (pinned to 12126d8942)

Solutions

  1. Convert to float/int first: Timestamp(float(s)).
  2. Use Timestamp.from_utc_datetime(dt) for datetime objects.
  3. Parse string timestamps with datetime.fromisoformat()/strptime, then convert.
  4. Cast numpy scalars with .item() or float() before constructing.

Example fix

# before
Timestamp('1699999999.5')
# after
Timestamp(float('1699999999.5'))
# or for datetime:
Timestamp.from_utc_datetime(datetime.datetime.utcnow())
Defensive patterns

Strategy: type-guard

Validate before calling

def as_timestamp(v):
    if isinstance(v, datetime.datetime):
        return Timestamp.from_utc_datetime(v)
    if isinstance(v, str):
        return Timestamp(float(v))
    if not isinstance(v, (int, float)):
        raise TypeError(f'non-numeric timestamp {v!r}')
    return Timestamp(v)

Type guard

def is_timestamp_input(v):
    return isinstance(v, (int, float)) and not isinstance(v, bool)

Try / catch

try:
    ts = Timestamp(seconds=v)
except TypeError as e:
    if 'as seconds' in str(e):
        ts = Timestamp(float(v))
    else:
        raise

Prevention

When it happens

Trigger: Constructing Timestamp(seconds=<non-numeric>) e.g. Timestamp('123'), Timestamp(datetime.now()), Timestamp(None), or passing a pandas/numpy non-int-float scalar into an API that builds a Timestamp from seconds.

Common situations: Feeding a timestamp string parsed from JSON/CSV directly into Timestamp; passing datetime objects instead of converting with Timestamp.from_utc_datetime(); numpy types (np.float32 usually fine via float, but object/Decimal dtypes are not).

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/7aa29791b9538865. Report an issue: GitHub.