Skip to content

REF: consolidate cast_from_unit checks #50852

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 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 15 additions & 33 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ def format_array_from_datetime(
return result


cdef int64_t _wrapped_cast_from_unit(object val, str unit) except? -1:
"""
Call cast_from_unit and re-raise OverflowError as OutOfBoundsDatetime
"""
# See also timedeltas._maybe_cast_from_unit
try:
return cast_from_unit(val, unit)
except OverflowError as err:
raise OutOfBoundsDatetime(
f"cannot convert input {val} with the unit '{unit}'"
) from err


def array_with_unit_to_datetime(
ndarray[object] values,
str unit,
Expand Down Expand Up @@ -261,13 +274,10 @@ def array_with_unit_to_datetime(
bint is_raise = errors=="raise"
ndarray[int64_t] iresult
object tz = None
bint is_ym
float fval

assert is_ignore or is_coerce or is_raise

is_ym = unit in "YM"

if unit == "ns":
result, tz = array_to_datetime(
values.astype(object, copy=False),
Expand All @@ -292,19 +302,7 @@ def array_with_unit_to_datetime(
if val != val or val == NPY_NAT:
iresult[i] = NPY_NAT
else:
if is_ym and is_float_object(val) and not val.is_integer():
# Analogous to GH#47266 for Timestamp
raise ValueError(
f"Conversion of non-round float with unit={unit} "
"is ambiguous"
)

try:
iresult[i] = cast_from_unit(val, unit)
except OverflowError:
raise OutOfBoundsDatetime(
f"cannot convert input {val} with the unit '{unit}'"
)
iresult[i] = _wrapped_cast_from_unit(val, unit)

elif isinstance(val, str):
if len(val) == 0 or val in nat_strings:
Expand All @@ -319,23 +317,7 @@ def array_with_unit_to_datetime(
f"non convertible value {val} with the unit '{unit}'"
)

if is_ym and not fval.is_integer():
# Analogous to GH#47266 for Timestamp
raise ValueError(
f"Conversion of non-round float with unit={unit} "
"is ambiguous"
)

try:
iresult[i] = cast_from_unit(fval, unit)
except ValueError:
raise ValueError(
f"non convertible value {val} with the unit '{unit}'"
)
except OverflowError:
raise OutOfBoundsDatetime(
f"cannot convert input {val} with the unit '{unit}'"
)
iresult[i] = _wrapped_cast_from_unit(fval, unit)

else:
# TODO: makes more sense as TypeError, but that would be an
Expand Down
16 changes: 9 additions & 7 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ cdef int64_t cast_from_unit(object ts, str unit) except? -1:
if ts is None:
return m

if unit in ["Y", "M"] and is_float_object(ts) and not ts.is_integer():
# GH#47267 it is clear that 2 "M" corresponds to 1970-02-01,
# but not clear what 2.5 "M" corresponds to, so we will
# disallow that case.
raise ValueError(
f"Conversion of non-round float with unit={unit} "
"is ambiguous"
)

# cast the unit, multiply base/frace separately
# to avoid precision issues from float -> int
base = <int64_t>ts
Expand Down Expand Up @@ -287,13 +296,6 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
# GH#47266 Avoid cast_from_unit, which would give weird results
# e.g. with "Y" and 150.0 we'd get 2120-01-01 09:00:00
return convert_to_tsobject(int(ts), tz, unit, False, False)
else:
# GH#47267 it is clear that 2 "M" corresponds to 1970-02-01,
# but not clear what 2.5 "M" corresponds to, so we will
# disallow that case.
raise ValueError(
f"Conversion of non-round float with unit={unit} is ambiguous."
)

ts = cast_from_unit(ts, unit)
obj.value = ts
Expand Down