Skip to content

Commit 996ce52

Browse files
committed
to_timedelta with ints result unit try to match input unit
1 parent 3af431f commit 996ce52

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

pandas/core/arrays/timedeltas.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,9 @@ def _ints_to_td64ns(data, unit: str = "ns") -> tuple[np.ndarray, bool]:
12051205
dtype_str = f"timedelta64[{unit}]"
12061206
data = data.view(dtype_str)
12071207

1208-
data = astype_overflowsafe(data, dtype=TD64NS_DTYPE)
1208+
new_dtype = get_supported_dtype(data.dtype)
1209+
if new_dtype != data.dtype:
1210+
data = astype_overflowsafe(data, dtype=new_dtype)
12091211

12101212
# the astype conversion makes a copy, so we can avoid re-copying later
12111213
copy_made = True

pandas/tests/groupby/methods/test_quantile.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,14 @@ def test_groupby_quantile_allNA_column(dtype):
363363

364364
def test_groupby_timedelta_quantile():
365365
# GH: 29485
366-
df = DataFrame(
367-
{"value": pd.to_timedelta(np.arange(4), unit="s"), "group": [1, 1, 2, 2]}
368-
)
366+
tdi = pd.to_timedelta(np.arange(4), unit="s").as_unit("us")
367+
df = DataFrame({"value": tdi, "group": [1, 1, 2, 2]})
369368
result = df.groupby("group").quantile(0.99)
370369
expected = DataFrame(
371370
{
372371
"value": [
373-
pd.Timedelta("0 days 00:00:00.990000").as_unit("ns"),
374-
pd.Timedelta("0 days 00:00:02.990000").as_unit("ns"),
372+
pd.Timedelta("0 days 00:00:00.990000"),
373+
pd.Timedelta("0 days 00:00:02.990000"),
375374
]
376375
},
377376
index=Index([1, 2], name="group"),

pandas/tests/io/json/test_pandas.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,8 @@ def test_timedelta(self):
11561156

11571157
with tm.assert_produces_warning(Pandas4Warning, match=msg):
11581158
json = frame.to_json()
1159-
tm.assert_frame_equal(frame, read_json(StringIO(json)).apply(converter))
1159+
result = read_json(StringIO(json)).apply(converter)
1160+
tm.assert_frame_equal(frame.astype("m8[ms]"), result)
11601161

11611162
def test_timedelta2(self):
11621163
frame = DataFrame(

pandas/tests/scalar/timedelta/test_constructors.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ def test_unit_deprecated(self, unit, unit_depr):
155155
def test_unit_parser(self, unit, np_unit, wrapper):
156156
# validate all units, GH 6855, GH 21762
157157
# array-likes
158+
if wrapper is list:
159+
# we go through _objects_to_td64ns which hasn't yet been updated
160+
exp_unit = "ns"
161+
else:
162+
exp_unit = np_unit if np_unit not in ["W", "D", "m"] else "s"
158163
expected = TimedeltaIndex(
159164
[np.timedelta64(i, np_unit) for i in np.arange(5).tolist()],
160-
dtype="m8[ns]",
165+
dtype=f"m8[{exp_unit}]",
161166
)
162167

163168
result = to_timedelta(wrapper(range(5)), unit=unit)

pandas/tests/tools/test_to_datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ def test_dataframe_field_aliases_column_subset(self, df, cache, unit):
21112111
result = to_datetime(df[list(unit.keys())].rename(columns=unit), cache=cache)
21122112
expected = Series(
21132113
[Timestamp("20150204 06:58:10"), Timestamp("20160305 07:59:11")],
2114-
dtype="M8[ns]",
2114+
dtype="M8[us]",
21152115
)
21162116
tm.assert_series_equal(result, expected)
21172117

pandas/tests/tools/test_to_timedelta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_to_timedelta_units_dtypes(self, dtype, unit):
121121
# arrays of various dtypes
122122
arr = np.array([1] * 5, dtype=dtype)
123123
result = to_timedelta(arr, unit=unit)
124-
exp_dtype = "m8[ns]" if dtype == "int64" else "m8[s]"
124+
exp_dtype = "m8[s]"
125125
expected = TimedeltaIndex([np.timedelta64(1, unit)] * 5, dtype=exp_dtype)
126126
tm.assert_index_equal(result, expected)
127127

@@ -277,7 +277,7 @@ def test_to_timedelta_coerce_strings_unit(self):
277277
)
278278
def test_to_timedelta_nullable_int64_dtype(self, expected_val, result_val):
279279
# GH 35574
280-
expected = Series([timedelta(days=1), expected_val], dtype="m8[ns]")
280+
expected = Series([timedelta(days=1), expected_val], dtype="m8[s]")
281281
result = to_timedelta(Series([1, result_val], dtype="Int64"), unit="days")
282282

283283
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)