Skip to content

Commit b248eb6

Browse files
committed
API: Timedelta constructor from keywords give microseconds
1 parent 53e778b commit b248eb6

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ class Timedelta(_Timedelta):
20452045
int(ns)
20462046
+ int(us * 1_000)
20472047
+ int(ms * 1_000_000)
2048-
+ seconds
2048+
+ seconds, "ns"
20492049
)
20502050
except OverflowError as err:
20512051
# GH#55503
@@ -2055,6 +2055,13 @@ class Timedelta(_Timedelta):
20552055
)
20562056
raise OutOfBoundsTimedelta(msg) from err
20572057

2058+
if (
2059+
"nanoseconds" not in kwargs
2060+
and cnp.get_timedelta64_value(value) % 1000 == 0
2061+
):
2062+
# If possible, give a microsecond unit
2063+
value = value.astype("m8[us]")
2064+
20582065
disallow_ambiguous_unit(unit)
20592066

20602067
cdef:

pandas/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def rand_series_with_duplicate_datetimeindex() -> Series:
938938
Timestamp("2011-01-01", tz="US/Eastern").as_unit("s"),
939939
DatetimeTZDtype(unit="s", tz="US/Eastern"),
940940
),
941-
(Timedelta(seconds=500), "timedelta64[ns]"),
941+
(Timedelta(seconds=500), "timedelta64[us]"),
942942
]
943943
)
944944
def ea_scalar_and_dtype(request):

pandas/tests/arithmetic/test_numeric.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ def test_div_td64arr(self, left, box_cls):
223223
@pytest.mark.parametrize(
224224
"scalar_td",
225225
[
226-
Timedelta(days=1),
227-
Timedelta(days=1).to_timedelta64(),
226+
Timedelta(days=1).as_unit("ns"),
227+
Timedelta(days=1).as_unit("ns").to_timedelta64(),
228228
Timedelta(days=1).to_pytimedelta(),
229229
Timedelta(days=1).to_timedelta64().astype("timedelta64[s]"),
230230
Timedelta(days=1).to_timedelta64().astype("timedelta64[ms]"),
@@ -254,9 +254,9 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box_with_array):
254254
@pytest.mark.parametrize(
255255
"scalar_td",
256256
[
257-
Timedelta(days=1),
258-
Timedelta(days=1).to_timedelta64(),
259-
Timedelta(days=1).to_pytimedelta(),
257+
Timedelta(days=1).as_unit("ns"),
258+
Timedelta(days=1).as_unit("ns").to_timedelta64(),
259+
Timedelta(days=1).as_unit("ns").to_pytimedelta(),
260260
],
261261
ids=lambda x: type(x).__name__,
262262
)

pandas/tests/base/test_value_counts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def test_value_counts_object_inference_deprecated():
359359
+ [Timestamp("2016-01-02")]
360360
+ [Timestamp("2016-01-01") + Timedelta(days=i) for i in range(1, 5)]
361361
),
362-
DatetimeIndex(pd.date_range("2016-01-01", periods=5, freq="D")),
362+
DatetimeIndex(pd.date_range("2016-01-01", periods=5, freq="D", unit="us")),
363363
],
364364
[
365365
TimedeltaIndex(

pandas/tests/scalar/timedelta/methods/test_as_unit.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ class TestAsUnit:
1010
def test_as_unit(self):
1111
td = Timedelta(days=1)
1212

13-
assert td.as_unit("ns") is td
13+
assert td.as_unit("us") is td
1414

15-
res = td.as_unit("us")
16-
assert res._value == td._value // 1000
17-
assert res._creso == NpyDatetimeUnit.NPY_FR_us.value
15+
res = td.as_unit("ns")
16+
assert res._value == td._value * 1000
17+
assert res._creso == NpyDatetimeUnit.NPY_FR_ns.value
1818

19-
rt = res.as_unit("ns")
19+
rt = res.as_unit("us")
2020
assert rt._value == td._value
2121
assert rt._creso == td._creso
2222

2323
res = td.as_unit("ms")
24-
assert res._value == td._value // 1_000_000
24+
assert res._value == td._value // 1_000
2525
assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
2626

27-
rt = res.as_unit("ns")
27+
rt = res.as_unit("us")
2828
assert rt._value == td._value
2929
assert rt._creso == td._creso
3030

3131
res = td.as_unit("s")
32-
assert res._value == td._value // 1_000_000_000
32+
assert res._value == td._value // 1_000_000
3333
assert res._creso == NpyDatetimeUnit.NPY_FR_s.value
3434

35-
rt = res.as_unit("ns")
35+
rt = res.as_unit("us")
3636
assert rt._value == td._value
3737
assert rt._creso == td._creso
3838

pandas/tests/scalar/timedelta/test_arithmetic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,11 @@ def test_mod_numeric(self):
818818
assert isinstance(result, Timedelta)
819819
assert result == Timedelta(0)
820820

821-
result = td % 1e12
821+
result = td % 1e9
822822
assert isinstance(result, Timedelta)
823823
assert result == Timedelta(minutes=3, seconds=20)
824824

825-
result = td % int(1e12)
825+
result = td % int(1e9)
826826
assert isinstance(result, Timedelta)
827827
assert result == Timedelta(minutes=3, seconds=20)
828828

@@ -876,8 +876,8 @@ def test_divmod_numeric(self):
876876
# GH#19365
877877
td = Timedelta(days=2, hours=6)
878878

879-
result = divmod(td, 53 * 3600 * 1e9)
880-
assert result[0] == Timedelta(1, unit="ns")
879+
result = divmod(td, 53 * 3600 * 1e6)
880+
assert result[0] == Timedelta(1, unit="us").as_unit("us")
881881
assert isinstance(result[1], Timedelta)
882882
assert result[1] == Timedelta(hours=1)
883883

pandas/tests/scalar/timedelta/test_constructors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ def test_construction():
272272
assert Timedelta(10, unit="D")._value == expected
273273
assert Timedelta(10.0, unit="D")._value == expected
274274
assert Timedelta("10 days")._value == expected
275-
assert Timedelta(days=10)._value == expected
276-
assert Timedelta(days=10.0)._value == expected
275+
assert Timedelta(days=10)._value == expected // 1000
276+
assert Timedelta(days=10.0)._value == expected // 1000
277277

278278
expected += np.timedelta64(10, "s").astype("m8[ns]").view("i8")
279279
assert Timedelta("10 days 00:00:10")._value == expected
280-
assert Timedelta(days=10, seconds=10)._value == expected
281-
assert Timedelta(days=10, milliseconds=10 * 1000)._value == expected
282-
assert Timedelta(days=10, microseconds=10 * 1000 * 1000)._value == expected
280+
assert Timedelta(days=10, seconds=10)._value == expected // 1000
281+
assert Timedelta(days=10, milliseconds=10 * 1000)._value == expected // 1000
282+
assert Timedelta(days=10, microseconds=10 * 1000 * 1000)._value == expected // 1000
283283

284284
# rounding cases
285285
assert Timedelta(82739999850000)._value == 82739999850000
@@ -411,7 +411,7 @@ def test_construction():
411411
def test_td_construction_with_np_dtypes(npdtype, item):
412412
# GH#8757: test construction with np dtypes
413413
pykwarg, npkwarg = item
414-
expected = np.timedelta64(1, npkwarg).astype("m8[ns]").view("i8")
414+
expected = np.timedelta64(1, npkwarg).astype("m8[us]").view("i8")
415415
assert Timedelta(**{pykwarg: npdtype(1)})._value == expected
416416

417417

pandas/tests/scalar/timedelta/test_timedelta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def test_resolution_deprecated(self):
664664
# GH#21344
665665
td = Timedelta(days=4, hours=3)
666666
result = td.resolution
667-
assert result == Timedelta(nanoseconds=1)
667+
assert result == Timedelta(microseconds=1)
668668

669669
# Check that the attribute is available on the class, mirroring
670670
# the stdlib timedelta behavior

0 commit comments

Comments
 (0)