Skip to content

ENH: Enable parsing of ISO8601-like timestamps with negative signs using pd.Timedelta (GH37172) #39497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Other enhancements
- :meth:`.Styler.set_tooltips_class` and :meth:`.Styler.set_table_styles` amended to optionally allow certain css-string input arguments (:issue:`39564`)
- :meth:`Series.loc.__getitem__` and :meth:`Series.loc.__setitem__` with :class:`MultiIndex` now raising helpful error message when indexer has too many dimensions (:issue:`35349`)
- :meth:`pandas.read_stata` and :class:`StataReader` support reading data from compressed files.

- Add support for parsing ``ISO 8601``-like timestamps with negative signs to :meth:`pandas.Timedelta` (:issue:`37172`)

.. ---------------------------------------------------------------------------

Expand Down
16 changes: 12 additions & 4 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ cdef convert_to_timedelta64(object ts, str unit):
ts = cast_from_unit(ts, unit)
ts = np.timedelta64(ts, "ns")
elif isinstance(ts, str):
if len(ts) > 0 and ts[0] == "P":
if (len(ts) > 0 and ts[0] == "P") or (len(ts) > 1 and ts[:2] == "-P"):
ts = parse_iso_format_string(ts)
else:
ts = parse_timedelta_string(ts)
Expand Down Expand Up @@ -673,13 +673,17 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1:
cdef:
unicode c
int64_t result = 0, r
int p = 0
int p = 0, sign = 1
object dec_unit = 'ms', err_msg
bint have_dot = 0, have_value = 0, neg = 0
list number = [], unit = []

err_msg = f"Invalid ISO 8601 Duration format - {ts}"

if ts[0] == "-":
sign = -1
ts = ts[1:]

for c in ts:
# number (ascii codes)
if 48 <= ord(c) <= 57:
Expand Down Expand Up @@ -711,6 +715,8 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1:
raise ValueError(err_msg)
else:
neg = 1
elif c == "+":
pass
elif c in ['W', 'D', 'H', 'M']:
if c in ['H', 'M'] and len(number) > 2:
raise ValueError(err_msg)
Expand Down Expand Up @@ -751,7 +757,7 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1:
# Received string only - never parsed any values
raise ValueError(err_msg)

return result
return sign*result


cdef _to_py_int_float(v):
Expand Down Expand Up @@ -1252,7 +1258,9 @@ class Timedelta(_Timedelta):
elif isinstance(value, str):
if unit is not None:
raise ValueError("unit must not be specified if the value is a str")
if len(value) > 0 and value[0] == 'P':
if (len(value) > 0 and value[0] == 'P') or (
len(value) > 1 and value[:2] == '-P'
):
value = parse_iso_format_string(value)
else:
value = parse_timedelta_string(value)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/scalar/timedelta/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ def test_construction_out_of_bounds_td64():
("P1W", Timedelta(days=7)),
("PT300S", Timedelta(seconds=300)),
("P1DT0H0M00000000000S", Timedelta(days=1)),
("PT-6H3M", Timedelta(hours=-6, minutes=3)),
("-PT6H3M", Timedelta(hours=-6, minutes=-3)),
("-PT-6H+3M", Timedelta(hours=6, minutes=-3)),
],
)
def test_iso_constructor(fmt, exp):
Expand All @@ -277,6 +280,8 @@ def test_iso_constructor(fmt, exp):
"P0DT999H999M999S",
"P1DT0H0M0.0000000000000S",
"P1DT0H0M0.S",
"P",
"-P",
],
)
def test_iso_constructor_raises(fmt):
Expand Down