Skip to content

BUG: to_datetime with errors coerce throws InvalidOperation decimal.DivisionImpossible #51084

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

Closed
3 tasks done
gkep opened this issue Jan 31, 2023 · 2 comments · Fixed by #51157
Closed
3 tasks done

BUG: to_datetime with errors coerce throws InvalidOperation decimal.DivisionImpossible #51084

gkep opened this issue Jan 31, 2023 · 2 comments · Fixed by #51157
Assignees
Labels
Milestone

Comments

@gkep
Copy link

gkep commented Jan 31, 2023

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
x = pd.to_datetime('87156549591102612381000001219H5',infer_datetime_format=True, errors='coerce')

Issue Description

Function should return NaT in case of problem, not exception decimal.DivisionImpossible:
InvalidOperation: [<class 'decimal.DivisionImpossible'>]

Problem present also in former versions of pandas.

Expected Behavior

x = pd.NaT

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : English_United States.1252

pandas : 1.5.0
numpy : 1.23.1
pytz : 2022.1
dateutil : 2.8.2
setuptools : 57.0.0
pip : 22.2.2
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 : 7.31.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.3
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@gkep gkep added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 31, 2023
@gkep gkep changed the title BUG: InvalidOperation decimal.DivisionImpossible in to_datetime with errors coerce BUG: to_datetime with errors coerce throws InvalidOperation decimal.DivisionImpossible Feb 1, 2023
@MarcoGorelli
Copy link
Member

thanks @gkep for the report

this comes from dateutil:

In [4]: from dateutil.parser import parse as du_parse

In [5]: du_parse('87156549591102612381000001219H5')
---------------------------------------------------------------------------
InvalidOperation                          Traceback (most recent call last)
Cell In[5], line 1
----> 1 du_parse('87156549591102612381000001219H5')

File ~/pandas-dev/.311venv/lib/python3.11/site-packages/dateutil/parser/_parser.py:1368, in parse(timestr, parserinfo, **kwargs)
   1366     return parser(parserinfo).parse(timestr, **kwargs)
   1367 else:
-> 1368     return DEFAULTPARSER.parse(timestr, **kwargs)

File ~/pandas-dev/.311venv/lib/python3.11/site-packages/dateutil/parser/_parser.py:640, in parser.parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    636 if default is None:
    637     default = datetime.datetime.now().replace(hour=0, minute=0,
    638                                               second=0, microsecond=0)
--> 640 res, skipped_tokens = self._parse(timestr, **kwargs)
    642 if res is None:
    643     raise ParserError("Unknown string format: %s", timestr)

File ~/pandas-dev/.311venv/lib/python3.11/site-packages/dateutil/parser/_parser.py:740, in parser._parse(self, timestr, dayfirst, yearfirst, fuzzy, fuzzy_with_tokens)
    736     value = None
    738 if value is not None:
    739     # Numeric token
--> 740     i = self._parse_numeric_token(l, i, info, ymd, res, fuzzy)
    742 # Check weekday
    743 elif info.weekday(l[i]) is not None:

File ~/pandas-dev/.311venv/lib/python3.11/site-packages/dateutil/parser/_parser.py:936, in parser._parse_numeric_token(self, tokens, idx, info, ymd, res, fuzzy)
    932     (idx, hms) = self._parse_hms(idx, tokens, info, hms_idx)
    933     if hms is not None:
    934         # TODO: checking that hour/minute/second are not
    935         # already set?
--> 936         self._assign_hms(res, value_repr, hms)
    938 elif idx + 2 < len_l and tokens[idx + 1] == ':':
    939     # HH:MM[:SS[.ss]]
    940     res.hour = int(value)

File ~/pandas-dev/.311venv/lib/python3.11/site-packages/dateutil/parser/_parser.py:1047, in parser._assign_hms(self, res, value_repr, hms)
   1044 if hms == 0:
   1045     # Hour
   1046     res.hour = int(value)
-> 1047     if value % 1:
   1048         res.minute = int(60*(value % 1))
   1050 elif hms == 1:

InvalidOperation: [<class 'decimal.DivisionImpossible'>]

I'd suggest reporting there

As for what to do on the pandas side, this should probably be caught, we just need to add this exception to

try:
parsed_datetime = du_parse(dt_str, dayfirst=dayfirst)
except (ValueError, OverflowError):
# In case the datetime can't be parsed, its format cannot be guessed
return None

, and add a test case for this in

@pytest.mark.parametrize(
"invalid_dt",
[
"01/2013",
"12:00:00",
"1/1/1/1",
"this_is_not_a_datetime",
"51a",
"13/2019",
"202001", # YYYYMM isn't ISO8601
"2020/01", # YYYY/MM isn't ISO8601 either
],
)
def test_guess_datetime_format_invalid_inputs(invalid_dt):
# A datetime string must include a year, month and a day for it to be
# guessable, in addition to being a string that looks like a datetime.
assert parsing.guess_datetime_format(invalid_dt) is None

Interested in submitting a PR?

@MarcoGorelli MarcoGorelli added good first issue Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 3, 2023
@MarcoGorelli MarcoGorelli added this to the 2.0 milestone Feb 3, 2023
@kcaashish
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants