Skip to content

Added improvements in to_datetime Error reporting message #47995

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

Merged
merged 9 commits into from
Aug 16, 2022
Merged
4 changes: 2 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ cpdef array_to_datetime(
continue
elif is_raise:
raise ValueError(
f"time data {val} doesn't match format specified"
f"time data \"{val}\" at position {i} doesn't match format specified"
)
return values, tz_out

Expand All @@ -611,7 +611,7 @@ cpdef array_to_datetime(
if is_coerce:
iresult[i] = NPY_NAT
continue
raise TypeError("invalid string coercion to datetime")
raise TypeError(f"invalid string coercion to datetime for \"{val}\" at position {i}")

if tz is not None:
seen_datetime_offset = True
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def parse_datetime_string(
datetime dt

if not _does_string_look_like_datetime(date_string):
raise ValueError('Given date string not likely a datetime.')
raise ValueError(f'Given date string {date_string} not likely a datetime')

if does_string_look_like_time(date_string):
# use current datetime as default, not pass _DEFAULT_DATETIME
Expand Down Expand Up @@ -320,7 +320,7 @@ def parse_datetime_string(
except TypeError:
# following may be raised from dateutil
# TypeError: 'NoneType' object is not iterable
raise ValueError('Given date string not likely a datetime.')
raise ValueError(f'Given date string {date_string} not likely a datetime')

return dt

Expand Down Expand Up @@ -396,7 +396,7 @@ cdef parse_datetime_string_with_reso(
int out_tzoffset

if not _does_string_look_like_datetime(date_string):
raise ValueError('Given date string not likely a datetime.')
raise ValueError(f'Given date string {date_string} not likely a datetime')

parsed, reso = _parse_delimited_date(date_string, dayfirst)
if parsed is not None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_invalid_arguments(self):
with pytest.raises(ValueError, match=msg):
Period(month=1)

msg = "Given date string not likely a datetime"
msg = "Given date string -2000 not likely a datetime"
with pytest.raises(ValueError, match=msg):
Period("-2000", "A")
msg = "day is out of range for month"
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def test_datetime_invalid_scalar(self, value, format, infer):
msg = (
"is a bad directive in format|"
"second must be in 0..59|"
"Given date string not likely a datetime"
f"Given date string {value} not likely a datetime"
)
with pytest.raises(ValueError, match=msg):
to_datetime(
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def test_datetime_invalid_index(self, values, format, infer):

msg = (
"is a bad directive in format|"
"Given date string not likely a datetime|"
f"Given date string {values[0]} not likely a datetime|"
"second must be in 0..59"
)
with pytest.raises(ValueError, match=msg):
Expand Down Expand Up @@ -2220,7 +2220,7 @@ def test_day_not_in_month_raise(self, cache):

@pytest.mark.parametrize("arg", ["2015-02-29", "2015-02-32", "2015-04-31"])
def test_day_not_in_month_raise_value(self, cache, arg):
msg = f"time data {arg} doesn't match format specified"
msg = f'time data "{arg}" at position 0 doesn\'t match format specified'
with pytest.raises(ValueError, match=msg):
to_datetime(arg, errors="raise", format="%Y-%m-%d", cache=cache)

Expand Down