Skip to content

Commit

Permalink
REF: Avoid np.can_cast for scalar inference for NEP 50 (#55707)
Browse files Browse the repository at this point in the history
* REF: Avoid np.can_cast for scalar inference for NEP 50

* Use helper function

* end->stop

* ignore typing

* Address NEP 50 later
  • Loading branch information
mroeschke authored Oct 27, 2023
1 parent 0cbe41a commit ff5cae7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ echo PYTHONHASHSEED=$PYTHONHASHSEED

COVERAGE="-s --cov=pandas --cov-report=xml --cov-append --cov-config=pyproject.toml"

PYTEST_CMD="MESONPY_EDITABLE_VERBOSE=1 PYTHONDEVMODE=1 PYTHONWARNDEFAULTENCODING=1 pytest -r fEs -n $PYTEST_WORKERS --dist=loadfile $TEST_ARGS $COVERAGE $PYTEST_TARGET"
# TODO: Support NEP 50 and remove NPY_PROMOTION_STATE
PYTEST_CMD="NPY_PROMOTION_STATE=legacy MESONPY_EDITABLE_VERBOSE=1 PYTHONDEVMODE=1 PYTHONWARNDEFAULTENCODING=1 pytest -r fEs -n $PYTEST_WORKERS --dist=loadfile $TEST_ARGS $COVERAGE $PYTEST_TARGET"

if [[ "$PATTERN" ]]; then
PYTEST_CMD="$PYTEST_CMD -m \"$PATTERN\""
Expand Down
25 changes: 23 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ 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):
if not np_can_cast_scalar(fill_value, dtype): # type: ignore[arg-type]
# upcast to prevent overflow
mst = np.min_scalar_type(fill_value)
dtype = np.promote_types(dtype, mst)
Expand Down Expand Up @@ -1916,4 +1916,25 @@ def _dtype_can_hold_range(rng: range, dtype: np.dtype) -> bool:
"""
if not len(rng):
return True
return np.can_cast(rng[0], dtype) and np.can_cast(rng[-1], dtype)
return np_can_cast_scalar(rng.start, dtype) and np_can_cast_scalar(rng.stop, dtype)


def np_can_cast_scalar(element: Scalar, dtype: np.dtype) -> bool:
"""
np.can_cast pandas-equivalent for pre 2-0 behavior that allowed scalar
inference
Parameters
----------
element : Scalar
dtype : np.dtype
Returns
-------
bool
"""
try:
np_can_hold_element(dtype, element)
return True
except (LossySetitemError, NotImplementedError):
return False

0 comments on commit ff5cae7

Please sign in to comment.