From a6c9aa757a0d5feb1d989d986bb60b3d5e466080 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Mon, 15 Aug 2022 22:37:10 +0530 Subject: [PATCH] Added improvements in to_datetime Error reporting message - incorrect field today/now shown in error (#47860) * Revert "addressing reviews CL#1" This reverts commit 69dc2d43875c5ffc307b829397612a66b0549316. * Added improvements in to_datetime Error reporting message - incorrect field now/today shown as error * Added improvements in to_datetime Error reporting message - incorrect field now/today shown as error * added testcase * adding position info & updated testcase * precommit format changes * addressing review comments * addressing review comments * addressing review comments * addressing review comments * addressing review comments #2 * addressing review comments --- pandas/_libs/tslib.pyx | 4 +++- pandas/_libs/tslibs/parsing.pyx | 8 ++++++++ pandas/tests/tools/test_to_datetime.py | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index b3e51191e8efa..fe4078f611f7e 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -799,6 +799,7 @@ cdef _array_to_datetime_object( # We return an object array and only attempt to parse: # 1) NaT or NaT-like values # 2) datetime strings, which we return as datetime.datetime + # 3) special strings - "now" & "today" for i in range(n): val = values[i] if checknull_with_nat_and_na(val) or PyDateTime_Check(val): @@ -817,7 +818,8 @@ cdef _array_to_datetime_object( yearfirst=yearfirst) pydatetime_to_dt64(oresult[i], &dts) check_dts_bounds(&dts) - except (ValueError, OverflowError): + except (ValueError, OverflowError) as ex: + ex.args = (f"{ex} present at position {i}", ) if is_coerce: oresult[i] = NaT continue diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index 97a8f81094a8f..1d5bbf87090eb 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -298,6 +298,14 @@ def parse_datetime_string( if dt is not None: return dt + # Handling special case strings today & now + if date_string == "now": + dt = datetime.now() + return dt + elif date_string == "today": + dt = datetime.today() + return dt + try: dt, _ = _parse_dateabbr_string(date_string, _DEFAULT_DATETIME, freq=None) return dt diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index aa5c3324fe0f0..4050817b39b88 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2591,6 +2591,13 @@ def test_invalid_origins_tzinfo(self): with pytest.raises(ValueError, match="must be tz-naive"): to_datetime(1, unit="D", origin=datetime(2000, 1, 1, tzinfo=pytz.utc)) + def test_incorrect_value_exception(self): + # GH47495 + with pytest.raises( + ValueError, match="Unknown string format: yesterday present at position 1" + ): + to_datetime(["today", "yesterday"]) + @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830