Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~

- Fixed reading of Parquet files with timezone-aware timestamps or localizing of a timeseries with a ``pytz`` timezone when an older version of ``pytz`` was installed (:issue:`64978`)
- Fixed regression in :func:`to_timedelta` ignoring the ``unit`` argument for round float values when mixed with non-round floats in a list (:issue:`65150`)

.. ---------------------------------------------------------------------------
.. _whatsnew_303.bug_fixes:
Expand Down
15 changes: 8 additions & 7 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,15 @@ def array_to_timedelta64(
if item == NPY_NAT:
ival = NPY_NAT
else:
ival = _numeric_to_td64ns(item, parsed_unit, int_reso)
# GH#65150 match the pattern in tslib.pyx: update creso
# first, then convert using creso as the output resolution.
item_reso = int_reso

state.update_creso(item_reso)
if infer_reso:
creso = state.creso

ival = _numeric_to_td64ns(item, parsed_unit, creso)

elif is_float_object(item):
int_item = int(item)
if item == int_item:
Expand All @@ -501,21 +503,20 @@ def array_to_timedelta64(
if item == NPY_NAT:
ival = NPY_NAT
else:
ival = _numeric_to_td64ns(item, parsed_unit, int_reso)
item_reso = int_reso

state.update_creso(item_reso)
if infer_reso:
creso = state.creso
else:
ival = _numeric_to_td64ns(item, parsed_unit, NPY_FR_ns)

ival = _numeric_to_td64ns(item, parsed_unit, creso)
else:
item_reso = NPY_FR_ns
int_reso = NPY_FR_ns
state.update_creso(item_reso)
if infer_reso:
creso = state.creso

ival = _numeric_to_td64ns(item, parsed_unit, creso)

else:
raise TypeError(f"Invalid type for timedelta scalar: {type(item)}")

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/tools/test_to_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ def test_to_timedelta_unit_non_round_floats(self):
result2 = to_timedelta(arr2, unit="s")
assert result2.unit == "ns"

def test_to_timedelta_unit_mixed_round_and_non_round_floats(self):
# GH#65150 - round floats mixed with non-round floats should
# respect the unit for all values
expected = to_timedelta(["0 days 00:00:01", "0 days 00:00:01.01"]).as_unit("ns")

result = to_timedelta([1.0, 1.01], unit="s")
tm.assert_index_equal(result, expected)

# Also test integers mixed with non-round floats
result2 = to_timedelta([1, 1.01], unit="s")
tm.assert_index_equal(result2, expected)


def test_from_numeric_arrow_dtype(any_numeric_ea_dtype):
# GH 52425
Expand Down
Loading