Skip to content

Commit fbf93da

Browse files
committed
Parse all components of ISO 8601 durations, even if S is not included
1 parent d8e8ad9 commit fbf93da

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,6 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1:
620620
if not len(unit):
621621
number.append(c)
622622
else:
623-
if 'H' in unit or 'M' in unit or 'W' in unit:
624-
if len(number) > 2:
625-
raise ValueError(err_msg)
626623
r = timedelta_from_spec(number, '0', unit)
627624
result += timedelta_as_neg(r, neg)
628625

@@ -638,6 +635,13 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1:
638635
neg = 1
639636
elif c in ['W', 'D', 'H', 'M']:
640637
unit.append(c)
638+
if c in ['H', 'M'] and len(number) > 2:
639+
raise ValueError(err_msg)
640+
r = timedelta_from_spec(number, '0', unit)
641+
result += timedelta_as_neg(r, neg)
642+
643+
neg = 0
644+
unit, number = [], []
641645
elif c == '.':
642646
# append any seconds
643647
if len(number):

pandas/tests/scalar/timedelta/test_constructors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ def test_overflow_on_construction():
231231
("PT1S", Timedelta(seconds=1)),
232232
("PT0S", Timedelta(seconds=0)),
233233
("P1WT0S", Timedelta(days=7, seconds=0)),
234+
("P1D", Timedelta(days=1)),
235+
("P1DT1H", Timedelta(days=1, hours=1)),
236+
("P1W", Timedelta(days=7)),
234237
],
235238
)
236239
def test_iso_constructor(fmt, exp):

0 commit comments

Comments
 (0)