Skip to content

Commit

Permalink
Added improvements in to_datetime Error reporting message - incorrect…
Browse files Browse the repository at this point in the history
… field today/now shown in error (pandas-dev#47860)

* Revert "addressing reviews CL#1"

This reverts commit 69dc2d4.

* 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
  • Loading branch information
dannyi96 authored and noatamir committed Nov 9, 2022
1 parent 31bedb0 commit 3d3f221
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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] = <object>NaT
continue
Expand Down
8 changes: 8 additions & 0 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d3f221

Please sign in to comment.