Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ class Timedelta(_Timedelta):
int(ns)
+ int(us * 1_000)
+ int(ms * 1_000_000)
+ seconds
+ seconds, "ns"
)
except OverflowError as err:
Copy link
Member

Choose a reason for hiding this comment

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

We should probably create some todo to try to parse to microseconds if this is out of bounds for nanoseconds?

# GH#55503
Expand All @@ -2055,6 +2055,13 @@ class Timedelta(_Timedelta):
)
raise OutOfBoundsTimedelta(msg) from err

if (
"nanoseconds" not in kwargs
and cnp.get_timedelta64_value(value) % 1000 == 0
):
# If possible, give a microsecond unit
value = value.astype("m8[us]")

disallow_ambiguous_unit(unit)

cdef:
Expand Down
2 changes: 1 addition & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ def rand_series_with_duplicate_datetimeindex() -> Series:
Timestamp("2011-01-01", tz="US/Eastern").as_unit("s"),
DatetimeTZDtype(unit="s", tz="US/Eastern"),
),
(Timedelta(seconds=500), "timedelta64[ns]"),
(Timedelta(seconds=500), "timedelta64[us]"),
]
)
def ea_scalar_and_dtype(request):
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ def test_div_td64arr(self, left, box_cls):
@pytest.mark.parametrize(
"scalar_td",
[
Timedelta(days=1),
Timedelta(days=1).to_timedelta64(),
Timedelta(days=1).as_unit("ns"),
Timedelta(days=1).as_unit("ns").to_timedelta64(),
Comment on lines +226 to +227
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering why this would be needed, but I suppose it is because the expected will have ns. But now the array inference PR is merged, this change can be (or needs to be) reverted?

Timedelta(days=1).to_pytimedelta(),
Timedelta(days=1).to_timedelta64().astype("timedelta64[s]"),
Timedelta(days=1).to_timedelta64().astype("timedelta64[ms]"),
Expand Down Expand Up @@ -254,9 +254,9 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box_with_array):
@pytest.mark.parametrize(
"scalar_td",
[
Timedelta(days=1),
Timedelta(days=1).to_timedelta64(),
Timedelta(days=1).to_pytimedelta(),
Timedelta(days=1).as_unit("ns"),
Timedelta(days=1).as_unit("ns").to_timedelta64(),
Timedelta(days=1).as_unit("ns").to_pytimedelta(),
],
ids=lambda x: type(x).__name__,
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def test_value_counts_object_inference_deprecated():
+ [Timestamp("2016-01-02")]
+ [Timestamp("2016-01-01") + Timedelta(days=i) for i in range(1, 5)]
),
DatetimeIndex(pd.date_range("2016-01-01", periods=5, freq="D")),
DatetimeIndex(pd.date_range("2016-01-01", periods=5, freq="D", unit="us")),
],
[
TimedeltaIndex(
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/scalar/timedelta/methods/test_as_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ class TestAsUnit:
def test_as_unit(self):
td = Timedelta(days=1)

assert td.as_unit("ns") is td
assert td.as_unit("us") is td

res = td.as_unit("us")
assert res._value == td._value // 1000
assert res._creso == NpyDatetimeUnit.NPY_FR_us.value
res = td.as_unit("ns")
assert res._value == td._value * 1000
assert res._creso == NpyDatetimeUnit.NPY_FR_ns.value

rt = res.as_unit("ns")
rt = res.as_unit("us")
assert rt._value == td._value
assert rt._creso == td._creso

res = td.as_unit("ms")
assert res._value == td._value // 1_000_000
assert res._value == td._value // 1_000
assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value

rt = res.as_unit("ns")
rt = res.as_unit("us")
assert rt._value == td._value
assert rt._creso == td._creso

res = td.as_unit("s")
assert res._value == td._value // 1_000_000_000
assert res._value == td._value // 1_000_000
assert res._creso == NpyDatetimeUnit.NPY_FR_s.value

rt = res.as_unit("ns")
rt = res.as_unit("us")
assert rt._value == td._value
assert rt._creso == td._creso

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,11 @@ def test_mod_numeric(self):
assert isinstance(result, Timedelta)
assert result == Timedelta(0)

result = td % 1e12
result = td % 1e9
assert isinstance(result, Timedelta)
assert result == Timedelta(minutes=3, seconds=20)

result = td % int(1e12)
result = td % int(1e9)
assert isinstance(result, Timedelta)
assert result == Timedelta(minutes=3, seconds=20)

Expand Down Expand Up @@ -876,8 +876,8 @@ def test_divmod_numeric(self):
# GH#19365
td = Timedelta(days=2, hours=6)

result = divmod(td, 53 * 3600 * 1e9)
assert result[0] == Timedelta(1, unit="ns")
result = divmod(td, 53 * 3600 * 1e6)
assert result[0] == Timedelta(1, unit="us").as_unit("us")
assert isinstance(result[1], Timedelta)
assert result[1] == Timedelta(hours=1)

Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/scalar/timedelta/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ def test_construction():
assert Timedelta(10, unit="D")._value == expected
assert Timedelta(10.0, unit="D")._value == expected
assert Timedelta("10 days")._value == expected
assert Timedelta(days=10)._value == expected
assert Timedelta(days=10.0)._value == expected
assert Timedelta(days=10)._value == expected // 1000
assert Timedelta(days=10.0)._value == expected // 1000

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

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


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def test_resolution_deprecated(self):
# GH#21344
td = Timedelta(days=4, hours=3)
result = td.resolution
assert result == Timedelta(nanoseconds=1)
assert result == Timedelta(microseconds=1)

# Check that the attribute is available on the class, mirroring
# the stdlib timedelta behavior
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/tseries/offsets/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_apply_out_of_range(request, tz_naive_fixture, _offset):
request.applymarker(
pytest.mark.xfail(reason="After GH#49737 t.tzinfo is None on CI")
)
assert str(t.tzinfo) == str(result.tzinfo)
assert str(t.tzinfo) == str(result.tzinfo), (t.tzinfo, result.tzinfo)

except OutOfBoundsDatetime:
pass
Expand Down
Loading