Skip to content

BUG: fix to_timedelta ignoring unit for mixed round/non-round floats#65170

Merged
jbrockmendel merged 3 commits intopandas-dev:mainfrom
jbrockmendel:bug-65150
Apr 20, 2026
Merged

BUG: fix to_timedelta ignoring unit for mixed round/non-round floats#65170
jbrockmendel merged 3 commits intopandas-dev:mainfrom
jbrockmendel:bug-65150

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

closes #65150

Summary

  • pd.to_timedelta([1.0, 1.01], unit="s") was treating 1.0 as 1 nanosecond instead of 1 second
  • Root cause: in array_to_timedelta64, when a mix of round and non-round floats triggers a resolution-mismatch second pass, integers and round floats were converted using int_reso (the unit's resolution) instead of creso (the target resolution), so their values were stored in the wrong units
  • Fix mirrors the pattern already used in tslib.pyx for datetime conversions: update creso first, then convert using creso as the output resolution

Test plan

  • Added regression test for mixed round/non-round float lists
  • All existing timedelta tests pass (912 passed)
  • Pre-commit hooks pass

🤖 Generated with Claude Code

@jorisvandenbossche
Copy link
Copy Markdown
Member

Can you add a whatsnew note?

jbrockmendel and others added 2 commits April 16, 2026 08:51
…andas-dev#65150)

Match the pattern used in tslib.pyx for numeric conversions: update
creso first, then convert using creso as the output resolution. This
ensures that when a resolution mismatch triggers a second pass, all
numeric values (integers, round floats, non-round floats) are converted
to the unified target resolution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jorisvandenbossche
Copy link
Copy Markdown
Member

We had actually already fixed the same issue before: #64068, just only tested it with the non-round float first ..

Comment thread pandas/tests/tools/test_to_timedelta.py Outdated
# GH#65150 - round floats mixed with non-round floats should
# respect the unit for all values
result = to_timedelta([1.0, 1.01], unit="s")
expected = to_timedelta(np.array([1.0, 1.01]), unit="s")
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.

This assumes that this works correctly for arrays (instead of lists), I don't know if that actually is tested separately?

Could also create expected with to_timedelta(["0 days 00:00:01", "0 days 00:00:01.01"]), that seems more explicit / safer anyway

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

will update


result = to_timedelta(arr, unit="ns", errors="coerce")
expected = TimedeltaIndex([pd.NaT])
expected = TimedeltaIndex([pd.NaT], dtype="m8[ns]")
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.

How is this change related exactly?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

its a side-effect, but it matches the to_datetime behavior

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.

It's the result that changes, not the expected? (that already correctly gives "s" on main)

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.

Yes, understood

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel merged commit 5fb98e2 into pandas-dev:main Apr 20, 2026
45 checks passed
@jbrockmendel jbrockmendel deleted the bug-65150 branch April 20, 2026 20:20
@lumberbot-app
Copy link
Copy Markdown

lumberbot-app Bot commented Apr 20, 2026

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 5fb98e26946d6c7021e3ad32d02abc9b2123d9ef
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #65170: BUG: fix to_timedelta ignoring unit for mixed round/non-round floats'
  1. Push to a named branch:
git push YOURFORK 3.0.x:auto-backport-of-pr-65170-on-3.0.x
  1. Create a PR against branch 3.0.x, I would have named this PR:

"Backport PR #65170 on branch 3.0.x (BUG: fix to_timedelta ignoring unit for mixed round/non-round floats)"

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.

@jorisvandenbossche
Copy link
Copy Markdown
Member

Manual backport -> #65313

Sharl0tteIsTaken added a commit to Sharl0tteIsTaken/pandas that referenced this pull request Apr 22, 2026
…h-origin

* upstream/main: (31 commits)
  DOC:Missing r in your (pandas-dev#65323)
  DOC: fix grammar in the .dt accessor section (pandas-dev#65325)
  REGR: restore rank() for ExtensionArrays with custom values for sorting (pandas-dev#64976)
  BUG: MultiIndex.get_loc returns scalar for unique key in non-unique index (pandas-dev#65234)
  BUG/TST: add test for _cast_pointwise_result robustness + fix some cases (pandas-dev#65318)
  BUG: fix .loc with tuple key on MultiIndex with IntervalIndex level (pandas-dev#65239)
  BUG: permit building from source with mingw (pandas-dev#64849)
  BUG: DataFrame.loc setitem with list-like value on single-column EA DataFrame (pandas-dev#65241)
  PERF: preserve block memory layout in Block.copy (GH#60469) (pandas-dev#65302)
  PERF: short-circuit sort_index(level=...) on monotonic non-MultiIndex (pandas-dev#65279)
  BUG: fix FloatingArray.astype(str) crash with distinguish_nan_and_na=True (pandas-dev#65038)
  BUG: fix to_timedelta ignoring unit for mixed round/non-round floats (pandas-dev#65170)
  BUG: DataFrame.loc preserves original index name when key is an Index (pandas-dev#65229)
  REF: continue moving freq management off DatetimeArray/TimedeltaArray (GH#24566) (pandas-dev#65285)
  REF: remove redundant BaseMaskedArray.map override (pandas-dev#65297)
  Bump github/codeql-action from 4.35.1 to 4.35.2 (pandas-dev#65310)
  Bump actions/setup-node from 6.3.0 to 6.4.0 (pandas-dev#65309)
  BUG: Fix formatters applied to wrong columns in truncated DataFrame.to_string (GH#35410) (pandas-dev#65288)
  PERF: optimize block consolidation (pandas-dev#64574)
  CLN: Replace no_default signature with False for allow_duplicates in insert and reset_index (pandas-dev#65146)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: pd.to_timedelta ignores unit value for certain float values

2 participants