Skip to content
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

pd.to_timedelta not parsing iso-formatted strings #21877

fjdiod opened this issue Jul 12, 2018 · 4 comments


None yet
4 participants
Copy link

commented Jul 12, 2018

Code Sample, a copy-pastable example if possible

>>> delta = pd.Timedelta(1e9).isoformat()
>>> pd.to_timedelta(delta)
Traceback (most recent call last):
  File "pandas/_libs/tslibs/timedeltas.pyx", line 480, in pandas._libs.tslibs.timedeltas.timedelta_from_spec
    unit = timedelta_abbrevs[unit.lower()]
KeyError: 'p'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sergey/projects/pandas/pandas/core/tools/", line 99, in to_timedelta
    box=box, errors=errors)
  File "/home/sergey/projects/pandas/pandas/core/tools/", line 145, in _coerce_scalar_to_timedelta_type
    result = convert_to_timedelta64(r, unit)
  File "pandas/_libs/tslibs/timedeltas.pyx", line 139, in pandas._libs.tslibs.timedeltas.convert_to_timedelta64
    cpdef convert_to_timedelta64(object ts, object unit):
  File "pandas/_libs/tslibs/timedeltas.pyx", line 186, in pandas._libs.tslibs.timedeltas.convert_to_timedelta64
    ts = np.timedelta64(parse_timedelta_string(ts))
  File "pandas/_libs/tslibs/timedeltas.pyx", line 345, in pandas._libs.tslibs.timedeltas.parse_timedelta_string
    r = timedelta_from_spec(number, frac, unit)
  File "pandas/_libs/tslibs/timedeltas.pyx", line 482, in pandas._libs.tslibs.timedeltas.timedelta_from_spec
    raise ValueError("invalid abbreviation: {0}".format(unit))
ValueError: invalid abbreviation: P

Problem description

#19191 added support for iso-formatted strings to pd.Timedelta constructor but pd.to_timedelta cannot parse such strings yet.

Expected Output

Output of pd.show_versions()


commit: bdb6168
python-bits: 64
OS: Linux
OS-release: 4.13.0-45-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None

pandas: 0.24.0.dev0+305.gbdb61688a
pytest: 3.6.2
pip: 10.0.1
setuptools: 39.2.0
Cython: 0.28.3
numpy: 1.14.5
scipy: 1.1.0
pyarrow: 0.9.0
xarray: 0.10.7
IPython: 6.4.0
sphinx: 1.7.5
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.5
feather: 0.4.0
matplotlib: 2.2.2
openpyxl: 2.5.4
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.5
lxml: 4.2.2
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.8
pymysql: 0.8.1
psycopg2: None
jinja2: 2.10
s3fs: 0.1.5
fastparquet: 0.1.5
pandas_gbq: None
pandas_datareader: None
gcsfs: 0.1.0


This comment has been minimized.

Copy link

commented Jul 13, 2018

I don't see why we shouldn't add support. Go for it! 🙂


This comment has been minimized.

Copy link

commented Jul 13, 2018

@fjdiod feel free to split off some of the work you've done in #21827 to a PR referencing this. If you are unsure how just ping


This comment has been minimized.

Copy link
Contributor Author

commented Jul 13, 2018

@WillAyd where to put tests for pd.to_timedelta? Should they go somewhere in tests/scalar/timedelta?


This comment has been minimized.

Copy link

commented Jul 15, 2018

scalar/timedelta/ and indexes/timedeltas/ are both good candidates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.