Skip to content

REF: Avoid np.can_cast for scalar inference for NEP 50 #55707

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 5 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
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
24 changes: 10 additions & 14 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):
dtype = np.dtype(np.object_)

elif issubclass(dtype.type, np.integer):
if not np.can_cast(fill_value, dtype):
try:
np_can_hold_element(dtype, fill_value)
Copy link
Member

@lithomas1 lithomas1 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should right a wrapper function for this to make our own can cast.

Then, we don't have to scatter try/catch's everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Wrapped in a helper function

except (LossySetitemError, NotImplementedError):
# upcast to prevent overflow
mst = np.min_scalar_type(fill_value)
dtype = np.promote_types(dtype, mst)
Expand Down Expand Up @@ -1751,9 +1753,14 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:

if dtype.kind in "iu":
if isinstance(element, range):
if _dtype_can_hold_range(element, dtype):
if not len(element):
return True
try:
np_can_hold_element(dtype, element.start)
np_can_hold_element(dtype, element.stop)
return element
raise LossySetitemError
except (LossySetitemError, NotImplementedError) as err:
raise LossySetitemError from err

if is_integer(element) or (is_float(element) and element.is_integer()):
# e.g. test_setitem_series_int8 if we have a python int 1
Expand Down Expand Up @@ -1906,14 +1913,3 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
raise LossySetitemError

raise NotImplementedError(dtype)


def _dtype_can_hold_range(rng: range, dtype: np.dtype) -> bool:
"""
_maybe_infer_dtype_type infers to int64 (and float64 for very large endpoints),
but in many cases a range can be held by a smaller integer dtype.
Check if this is one of those cases.
"""
if not len(rng):
return True
return np.can_cast(rng[0], dtype) and np.can_cast(rng[-1], dtype)