Skip to content

[Python] Round float seconds to the nearest microsecond in Duration - #40236

Open
anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/timestamp-float-seconds-rounding
Open

anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/timestamp-float-seconds-rounding

Conversation

@anishmehta24

@anishmehta24 anishmehta24 commented Sep 23, 2026 •

Copy link
Copy Markdown

Duration(seconds) truncates instead of rounding when seconds is a float, so it can land one microsecond below the value passed in:

>>> Duration(2.000002).micros
2000001          # expected 2000002

seconds * 1000000 is a float multiplication that often lands just under the exact integer (2.000002 * 1e6 == 2000001.9999999998) and int() truncates it. #40257 fixed this for the Timestamp constructor; Duration.__init__ had the same line, so this uses round() there too, same as the Timestamp fix (micros keeps int(), matching it).

Test: DurationTest.test_constructor_float_rounds_to_nearest covers positive, negative, Duration.of, an exact value, and microsecond values round-tripped through float seconds. It fails on master. apache_beam/utils/timestamp_test.py 43 passed, yapf clean.

Fixes #40263

@anishmehta24
anishmehta24 force-pushed the fix/timestamp-float-seconds-rounding branch from 9da5881 to 5eb2e27 Compare September 23, 2026 10:16
@anishmehta24 anishmehta24 changed the title [Python] Round float seconds to the nearest unit in Timestamp [Python] Round float seconds to the nearest unit in Timestamp and Duration Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@anishmehta24

Copy link
Copy Markdown
Author

assign set of reviewers

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@anishmehta24
anishmehta24 force-pushed the fix/timestamp-float-seconds-rounding branch from 5eb2e27 to e4966b0 Compare September 24, 2026 16:11
@anishmehta24 anishmehta24 changed the title [Python] Round float seconds to the nearest unit in Timestamp and Duration [Python] Round float seconds to the nearest microsecond in Duration Sep 24, 2026
@anishmehta24

Copy link
Copy Markdown
Author

#40257 fixed the Timestamp half of this, so I've narrowed this PR to Duration, which still truncates the same way (Duration(2.000002).micros == 2000001 on master). Opened #40263 for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Duration(float seconds) truncates instead of rounding, losing a microsecond

1 participant