Skip to content

Commit

Permalink
addressing reviews CL#1
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyi96 committed Jul 24, 2022
1 parent f024141 commit ec59a4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
22 changes: 15 additions & 7 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ cpdef array_to_datetime(
# to check if all arguments have the same tzinfo
tz = py_dt.utcoffset()

except (ValueError, OverflowError):
except (ValueError, OverflowError) as err:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise TypeError("invalid string coercion to datetime")
raise type(err)(f"invalid string coercion to datetime for \"{val}\" at position {i}")

if tz is not None:
seen_datetime_offset = True
Expand Down Expand Up @@ -798,6 +798,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 @@ -811,17 +812,24 @@ cdef _array_to_datetime_object(
if len(val) == 0 or val in nat_strings:
oresult[i] = 'NaT'
continue

try:
oresult[i] = parse_datetime_string(val, dayfirst=dayfirst,
# Handling special case strings today & now
if val == "today":
oresult[i] = datetime.today()
elif val == "now":
oresult[i] = datetime.now()
else:
oresult[i] = parse_datetime_string(val, dayfirst=dayfirst,
yearfirst=yearfirst)
pydatetime_to_dt64(oresult[i], &dts)
check_dts_bounds(&dts)
except (ValueError, OverflowError):
pydatetime_to_dt64(oresult[i], &dts)
check_dts_bounds(&dts)
except (ValueError, OverflowError) as err:
if is_coerce:
oresult[i] = <object>NaT
continue
if is_raise:
raise ValueError(f"Unable to parse string \"{val}\" at position {i}")
raise type(err)(f"Unable to parse string \"{val}\" at position {i}")
return values, None
else:
if is_raise:
Expand Down
11 changes: 3 additions & 8 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,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 All @@ -282,17 +282,12 @@ def parse_datetime_string(
pass

try:
if date_string == "today":
dt = datetime.today()
elif date_string == "now":
dt = datetime.now()
else:
dt = du_parse(date_string, default=_DEFAULT_DATETIME,
dayfirst=dayfirst, yearfirst=yearfirst, **kwargs)
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 @@ -368,7 +363,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

0 comments on commit ec59a4f

Please sign in to comment.