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

BUG: Incorrect parsing of ISO 8601 durations strings in Timedelta constructor #51882

Open
3 tasks done
rosstitmarsh opened this issue Mar 10, 2023 · 1 comment
Open
3 tasks done
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Timedelta Timedelta data type

Comments

@rosstitmarsh
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# Raises an error for >2 digit hours or minutes
pd.Timedelta('PT100H')
pd.Timedelta('PT100M')

# Incorrectly parses any decimal component apart from seconds
pd.Timedelta('PT3.5H') == pd.Timedelta('PT5H3S')

# Raises an error for comma as decimal separator
pd.Timedelta('PT3,5H')

# Does not raise an error for plusses in numbers
pd.Timedelta('P+1S') == pd.Timedelta('PT1S')

# Does not raise an error for unsupported month component, instead parses it as minutes
pd.Timedelta('P1M') == pd.Timedelta('PT1M')

# Does not raise an error for hour and second components before the 'T' seperator
pd.Timedelta('P1H') == pd.Timedelta('PT1H')
pd.Timedelta('P1S') == pd.Timedelta('PT1S')

# Does not raise an error for day components after the 'T' seperator
pd.Timedelta('PT1D') == pd.Timedelta('P1D')

# Does not raise an error for duplicate components (and produces unexpected result)
pd.Timedelta('PT1S1S1S1S') == pd.Timedelta('PT20M34S')

# Does not raise an error for components in the incorrect order
pd.Timedelta('PT1M1H') == pd.Timedelta('PT1H1M')

# Does not raise an error for trailing 'T' separator
pd.Timedelta('P1DT') == pd.Timedelta('P1D')

# Does not raise an error for duplicate 'T' separators
pd.Timedelta('PTTT1H') == pd.Timedelta('PT1H')

# Does not raise an error for 'P' or 'T' in any position
pd.Timedelta('PTP1TPTH') == pd.Timedelta('PT1H')

Issue Description

Numbers

Components

  • Parses month (M) components as minutes.
  • Parses hour (H) or second (S) components before the T
  • Parses day components after the T
  • Parses duplicate components with weird results
  • Parses components in the incorrect order

P and T symbols

Expected Behavior

The follwing should all parse without errors.

pd.Timedelta("PT100H") == pd.Timedelta(hours=100)
pd.Timedelta("PT100M") == pd.Timedelta(minutes=100)
pd.Timedelta("PT3,5H") == pd.Timedelta(hours=3.5)

The follwing should all raise errors.

# Bad numbers
pd.Timedelta("PT.1S")
pd.Timedelta("PT+1S")

# Bad components
pd.Timedelta("P1M") 
pd.Timedelta("P1H")
pd.Timedelta("P1S")
pd.Timedelta("PT1D")
pd.Timedelta("PT1M1H")
pd.Timedelta("P1S1S")

# Misplaced P or T
pd.Timedelta("P1PD")
pd.Timedelta("P1TD")
pd.Timedelta("P1DT")

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.11.1.final.0
python-bits : 64

pandas : 1.5.3
numpy : 1.24.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@rosstitmarsh
Copy link
Author

Also covers #48122

@lithomas1 lithomas1 added Timedelta Timedelta data type Constructors Series/DataFrame/Index/pd.array Constructors and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants