BUG: fix to_timedelta ignoring unit for mixed round/non-round floats#65170
BUG: fix to_timedelta ignoring unit for mixed round/non-round floats#65170jbrockmendel merged 3 commits intopandas-dev:mainfrom
Conversation
|
Can you add a whatsnew note? |
…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>
|
We had actually already fixed the same issue before: #64068, just only tested it with the non-round float first .. |
| # 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") |
There was a problem hiding this comment.
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
|
|
||
| result = to_timedelta(arr, unit="ns", errors="coerce") | ||
| expected = TimedeltaIndex([pd.NaT]) | ||
| expected = TimedeltaIndex([pd.NaT], dtype="m8[ns]") |
There was a problem hiding this comment.
How is this change related exactly?
There was a problem hiding this comment.
its a side-effect, but it matches the to_datetime behavior
There was a problem hiding this comment.
It's the result that changes, not the expected? (that already correctly gives "s" on main)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Owee, I'm MrMeeseeks, Look at me. There seem to be a conflict, please backport manually. Here are approximate instructions:
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 If these instructions are inaccurate, feel free to suggest an improvement. |
|
Manual backport -> #65313 |
…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) ...
closes #65150
Summary
pd.to_timedelta([1.0, 1.01], unit="s")was treating1.0as 1 nanosecond instead of 1 secondarray_to_timedelta64, when a mix of round and non-round floats triggers a resolution-mismatch second pass, integers and round floats were converted usingint_reso(the unit's resolution) instead ofcreso(the target resolution), so their values were stored in the wrong unitstslib.pyxfor datetime conversions: updatecresofirst, then convert usingcresoas the output resolutionTest plan
🤖 Generated with Claude Code