Skip to content

BUG: Timedelta.round() raises ZeroDivisionError when internal unit is 's' and target frequency is sub-second#64836

Merged
jbrockmendel merged 7 commits into
pandas-dev:mainfrom
kjmin622:issue64828
Apr 10, 2026
Merged

BUG: Timedelta.round() raises ZeroDivisionError when internal unit is 's' and target frequency is sub-second#64836
jbrockmendel merged 7 commits into
pandas-dev:mainfrom
kjmin622:issue64828

Conversation

@kjmin622

Copy link
Copy Markdown
Contributor


def test_round_freq_finer_than_resolution(self):
# GH#64828
td = Timedelta(1.0, unit="days")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you assert td.unit == "s"

Comment thread pandas/_libs/tslibs/timedeltas.pyx Outdated
_Timedelta td_ns

unit = get_unit_for_round(freq, self._creso)
if unit == 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it would be simpler to match the Timestamp._round version:

        freq = to_offset(freq, is_period=False)
        nanos = get_unit_for_round(freq, self._creso)
        if nanos == 0:
            if freq.nanos == 0:
                raise ValueError("Division by zero in rounding")

            # e.g. self.unit == "s" and sub-second freq
            return self

@jorisvandenbossche jorisvandenbossche added Regression Functionality that used to work in a prior pandas version Non-Nano datetime64/timedelta64 with non-nanosecond resolution labels Mar 30, 2026
@jorisvandenbossche jorisvandenbossche modified the milestones: 3.0.2, 3.0.3 Mar 30, 2026
Comment thread doc/source/whatsnew/v3.0.2.rst Outdated
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`HDFStore.select` where the ``where`` clause on a datetime index silently returned empty results when the index had non-nanosecond resolution (:issue:`64310`)
- Fixed regression in :meth:`Series.interpolate` where ``limit_direction="both"`` with ``limit`` greater than the Series length raised ``ValueError`` (:issue:`64322`)
- Fixed regression in :meth:`Timedelta.round`, :meth:`Timedelta.floor`, and :meth:`Timedelta.ceil` raising ``ZeroDivisionError`` for sub-second ``freq`` (:issue:`64828`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.0.2 was released today. this will need to be moved to the 3.1 file

@kjmin622

kjmin622 commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

@jbrockmendel Thank you for your reviews! I applied those.

Comment thread pandas/_libs/tslibs/timedeltas.pyx Outdated
ndarray[int64_t] arr

unit = get_unit_for_round(freq, self._creso)
from pandas._libs.tslibs.offsets import to_offset

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you cimport this at the top?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than this LGTM. ping on green

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel I moved cimport to the top. Thank you.

Comment thread pandas/_libs/tslibs/timedeltas.pyx Outdated
from pandas._libs.tslibs.offsets import (
Day,
to_offset,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think to_offset needs to be a cimport or else you get a circular import problem

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel I applied it. Thank you!

@jbrockmendel jbrockmendel merged commit 16ca7d9 into pandas-dev:main Apr 10, 2026
45 checks passed
@jbrockmendel

Copy link
Copy Markdown
Member

thanks @kjmin622

@lumberbot-app

lumberbot-app Bot commented Apr 10, 2026

Copy link
Copy Markdown

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 3.0.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 16ca7d9d790698ad433b0982e92ee2846e42650a
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am "Backport PR #64836: BUG: Timedelta.round() raises ZeroDivisionError when internal unit is 's' and target frequency is sub-second"
  1. Push to a named branch:
git push YOURFORK 3.0.x:auto-backport-of-pr-64836-on-3.0.x
  1. Create a PR against branch 3.0.x, I would have named this PR:

"Backport PR #64836 on branch 3.0.x (BUG: Timedelta.round() raises ZeroDivisionError when internal unit is 's' and target frequency is sub-second)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@jbrockmendel

Copy link
Copy Markdown
Member

@kjmin622 can you handle the backport; follow the instructions above

Sharl0tteIsTaken added a commit to Sharl0tteIsTaken/pandas that referenced this pull request Apr 12, 2026
…-comparison

* upstream/main:
  PERF: use lookup instead of hash_inner_join for merge with unique right keys (pandas-dev#64691)
  BUG : update `SeriesGroupBy.ohlc()` to honor `as_index=False` (pandas-dev#65141)
  PERF: Use DataFrame-level reductions in DataFrame.agg with list of funcs (pandas-dev#65031)
  DOC: document required external libraries in read_* I/O docstrings (pandas-dev#65143)
  DOC: improve MultiIndex.is_monotonic_increasing/decreasing docstrings (pandas-dev#65154)
  BUG: Raise ValueError for non-boolean numeric_only in DataFrame/Series reductions (GH#53098) (pandas-dev#65131)
  BUG: Timedelta.round() raises ZeroDivisionError when internal unit is 's' and target frequency is sub-second (pandas-dev#64836)
  ENH: Add replace method to Index (closes pandas-dev#19495) (pandas-dev#65099)
  PERF: improve StringArray.isna (pandas-dev#57733)
  BUG: read parquet files with older pytz (DEP: keep lower pytz minimum version) (pandas-dev#65133)
  DEPR: deprecate dates-with-datetime64 in _maybe_downcast_for_indexing (pandas-dev#64871)
  DOC: note that DataFrame.values is not writeable (pandas-dev#65142)
  CLN: Update groupby observed defaults (pandas-dev#65148)
  PERF: avoid materializing values[indexer] in Block.setitem (pandas-dev#64251)
  DOC: update GroupBy.sum/min/max See Also sections (pandas-dev#65144)
kjmin622 added a commit to kjmin622/pandas that referenced this pull request Apr 13, 2026
… 's' and target frequency is sub-second (pandas-dev#64836)

(cherry picked from commit 16ca7d9)
@kjmin622 kjmin622 deleted the issue64828 branch April 13, 2026 04:35
@kjmin622

Copy link
Copy Markdown
Contributor Author

@jbrockmendel I have submitted a backport PR. #65201

jbrockmendel pushed a commit that referenced this pull request Apr 13, 2026
…oDivisionError when internal unit is 's' and target frequency is sub-second) (#65201)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Non-Nano datetime64/timedelta64 with non-nanosecond resolution Regression Functionality that used to work in a prior pandas version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Timedelta.round() raises ZeroDivisionError when internal unit is 's' and target frequency is sub-second

3 participants