apache/beam · error · TypeError

Cannot interpret %s %s as subseconds.

Error message

Cannot interpret %s %s as subseconds.

What it means

Timestamp.__init__ also validates the subseconds argument: it must be an int or float. Passing any other type raises this TypeError. (Note the same class raises the analogous messages for seconds and precision.)

Source

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

  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(
            'micros and subseconds are mutually exclusive, got micros=%s, '
            'subseconds=%s.' % (micros, subseconds))
      if precision != Timestamp.MICROS_PRECISION:

View on GitHub (pinned to 12126d8942)

Solutions

  1. Cast the value: Timestamp(seconds=s, subseconds=float(sub)).
  2. Parse numeric strings before passing.
  3. Replace None with 0 (the default) when subseconds is absent.
  4. Convert Decimal/np scalars via float()/item().

Example fix

# before
Timestamp(seconds=10, subseconds='0.25')
# after
Timestamp(seconds=10, subseconds=float('0.25'))
Defensive patterns

Strategy: type-guard

Validate before calling

def subseconds_or_zero(v):
    return float(v) if isinstance(v, (int, float, str)) else 0.0
Timestamp(seconds=s, subseconds=subseconds_or_zero(sub))

Type guard

def is_numeric_subseconds(v):
    return v is None or isinstance(v, (int, float))

Try / catch

try:
    ts = Timestamp(seconds=s, subseconds=sub)
except TypeError as e:
    if 'as subseconds' in str(e):
        ts = Timestamp(seconds=s, subseconds=float(sub))
    else:
        raise

Prevention

When it happens

Trigger: Constructing Timestamp(seconds=..., subseconds=<non-numeric>) e.g. subseconds='5', subseconds=None, or a Decimal/str fraction passed through from user input or serialized data.

Common situations: String fractional seconds from JSON ('0.5') passed directly; Decimal values from financial data; None used as a default for subseconds; numpy str_ scalars.

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/a9bbd2bcb9f00184. Report an issue: GitHub.