Skip to content

Commit cafd7dd

Browse files
committed
ENH: enhanced the Timedelta.total_seconds() override for compatibility reasons
DOC: updated the docstring of the `Timedelta.total_seconds()` TST: updated respective testing
1 parent 71a3ee6 commit cafd7dd

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

pandas/_libs/tslibs/nattype.pyx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,32 @@ class NaTType(_NaT):
442442
Monday == 1 ... Sunday == 7.
443443
""",
444444
)
445-
total_seconds = _make_nan_func("total_seconds", timedelta.total_seconds.__doc__)
445+
total_seconds = _make_nan_func(
446+
"total_seconds",
447+
"""
448+
Total seconds in the duration with default us precision
449+
(for compatibility with `datetime.timedelta`).
450+
451+
Parameters
452+
----------
453+
ns_precision : bool, default False
454+
Return the duration with ns precision.
455+
456+
Examples
457+
--------
458+
>>> td = pd.Timedelta(days=6, minutes=50, seconds=3,
459+
... milliseconds=10, microseconds=10, nanoseconds=12)
460+
>>> td
461+
Timedelta('6 days 00:50:03.010010012')
462+
463+
>>> td.total_seconds()
464+
521403.01001
465+
466+
>>> td.total_seconds(ns_precision=True)
467+
521403.010010012
468+
469+
"""
470+
)
446471
month_name = _make_nan_func(
447472
"month_name",
448473
"""

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,9 +1539,34 @@ class Timedelta(_Timedelta):
15391539
div = other // self
15401540
return div, other - div * self
15411541

1542-
def total_seconds(self):
1543-
"""Total seconds in the duration."""
1544-
return self.value / 1_000_000_000
1542+
# GH45129
1543+
def total_seconds(self, *, ns_precision=False) -> float:
1544+
"""
1545+
Total seconds in the duration with default us precision
1546+
(for compatibility with `datetime.timedelta`).
1547+
1548+
Parameters
1549+
----------
1550+
ns_precision : bool, default False
1551+
Return the duration with ns precision.
1552+
1553+
Examples
1554+
--------
1555+
>>> td = pd.Timedelta(days=6, minutes=50, seconds=3,
1556+
... milliseconds=10, microseconds=10, nanoseconds=12)
1557+
>>> td
1558+
Timedelta('6 days 00:50:03.010010012')
1559+
1560+
>>> td.total_seconds()
1561+
521403.01001
1562+
1563+
>>> td.total_seconds(ns_precision=True)
1564+
521403.010010012
1565+
1566+
"""
1567+
if ns_precision:
1568+
return self.value / 1_000_000_000
1569+
return super().total_seconds()
15451570

15461571

15471572
cdef bint is_any_td_scalar(object obj):

pandas/tests/tslibs/test_timedeltas.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ def test_huge_nanoseconds_overflow():
5050

5151
# GH40946
5252
@pytest.mark.parametrize(
53-
"obj, expected",
53+
"obj, expected_ns, expected_us",
5454
[
55-
(Timedelta("1us"), 1e-6),
56-
(Timedelta("500ns"), 5e-7),
57-
(Timedelta(nanoseconds=500), 5e-7),
58-
(Timedelta(seconds=1, nanoseconds=500), 1 + 5e-7),
59-
# require GH45108
60-
# (Timedelta(seconds=1e-9, milliseconds=1e-5, microseconds=1e-1), 111e-9),
61-
# (
62-
# Timedelta(days=1, seconds=1e-9, milliseconds=1e-5, microseconds=1e-1),
63-
# 24 * 3600 + 111e-9
64-
# )
55+
(Timedelta("1us"), 1e-6, 1e-6),
56+
(Timedelta("500ns"), 5e-7, 0.0),
57+
(Timedelta(nanoseconds=500), 5e-7, 0.0),
58+
(Timedelta(seconds=1, nanoseconds=500), 1 + 5e-7, 1.0),
59+
(Timedelta(seconds=1e-9, milliseconds=1e-5, microseconds=1e-1), 111e-9, 0.0),
60+
(
61+
Timedelta(days=1, seconds=1e-9, milliseconds=1e-5, microseconds=1e-1),
62+
24 * 3600 + 111e-9,
63+
24 * 3600,
64+
),
6565
],
6666
)
67-
def test_total_seconds(obj, expected):
68-
assert obj.total_seconds() == expected
67+
def test_total_seconds(obj: Timedelta, expected_ns, expected_us):
68+
assert obj.total_seconds() == expected_us
69+
assert obj.total_seconds(ns_precision=True) == expected_ns

0 commit comments

Comments
 (0)