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

Added improvements in to_datetime Error reporting message - incorrect field today/now shown in error #47860

Merged
merged 18 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ from pandas._libs.tslibs.util cimport (
get_c_string_buf_and_size,
is_array,
)
from pandas._libs.tslibs.timestamps import Timestamp


cdef extern from "../src/headers/portable.h":
Expand Down Expand Up @@ -298,6 +299,11 @@ def parse_datetime_string(
if dt is not None:
return dt

# Handling special case strings today & now
dt = _parse_today_now(date_string)
if dt is not None:
return dt

try:
dt, _ = _parse_dateabbr_string(date_string, _DEFAULT_DATETIME, freq=None)
return dt
Expand Down Expand Up @@ -1210,3 +1216,25 @@ cpdef str get_rule_month(str source):
return "DEC"
else:
return source.split("-")[1]

cdef inline object _parse_today_now(str date_string):
"""
Parse special case date inputs - "today", "now"
and if present return corresponding datetime object

Parameters
----------
date_string : str

Returns:
--------
datetime or None
"""
cdef:
object res = None

if date_string == "now":
res = Timestamp.utcnow()
elif date_string == "today":
res = Timestamp.today()
return res
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