Skip to content

Commit 9292728

Browse files
committed
Fix parsing of ISO8601 strings with empty period
1 parent c2a96c6 commit 9292728

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,7 @@ 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 in days, pop trailing T
624-
if unit[-1] == 'T':
625-
unit.pop()
626-
elif 'H' in unit or 'M' in unit:
623+
if 'H' in unit or 'M' in unit:
627624
if len(number) > 2:
628625
raise ValueError(err_msg)
629626
r = timedelta_from_spec(number, '0', unit)
@@ -632,14 +629,14 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1:
632629
neg = 0
633630
unit, number = [], [c]
634631
else:
635-
if c == 'P':
636-
pass # ignore leading character
632+
if c == 'P' or c == 'T':
633+
pass # ignore marking characters P and T
637634
elif c == '-':
638635
if neg or have_value:
639636
raise ValueError(err_msg)
640637
else:
641638
neg = 1
642-
elif c in ['D', 'T', 'H', 'M']:
639+
elif c in ['D', 'H', 'M']:
643640
unit.append(c)
644641
elif c == '.':
645642
# append any seconds

pandas/tests/scalar/timedelta/test_constructors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ def test_overflow_on_construction():
228228
("P0DT0H0M0.001S", Timedelta(milliseconds=1)),
229229
("P0DT0H1M0S", Timedelta(minutes=1)),
230230
("P1DT25H61M61S", Timedelta(days=1, hours=25, minutes=61, seconds=61)),
231+
("PT1S", Timedelta(seconds=1)),
232+
("PT0S", Timedelta(seconds=0)),
231233
],
232234
)
233235
def test_iso_constructor(fmt, exp):

0 commit comments

Comments
 (0)