Skip to content

Commit

Permalink
Reorder tests in maybe_downcast_numeric (#55825)
Browse files Browse the repository at this point in the history
* Reorder tests in maybe_downcast_numeric

The comment `# if we have any nulls, then we are done` is not consistent with the test `if isna(arr).any()` because `arr` is constructed only from the first element (`r[0]`) not the full ravel'd list of values.  Moreover, calling `np.array()` on some random type can have surprising consequences.

So instead, do the early-out test as intended, just using `r[0]` without going through `np.array()`.  Then test other things about `r[0]`.  Only then should we test all the values (and if we have any nulls, then we are done).

See #55824

Signed-off-by: Michael Tiemann <[email protected]>

* Simplify/optimize tests for downcasting

If the first element of `result` is an array, ravel that to get element we will test.  Otherwise use it as is.

We only need to check whether `result` is all non-null once.

Signed-off-by: Michael Tiemann <[email protected]>

* Update cast.py

Don't use deprecated array indexing on ExtensionArrays.  We need to now us `iloc`.

Signed-off-by: Michael Tiemann <[email protected]>

* Eliminate need to call `ravel`

When processing a multidimensional `ndarray`, we can get the first element by calling `result.item(0)` and completely avoid the copying needed by `ravel` to get the first element that way.  We can also eliminates an additional conditional check.

Signed-off-by: Michael Tiemann <[email protected]>

---------

Signed-off-by: Michael Tiemann <[email protected]>
Co-authored-by: Richard Shadrach <[email protected]>
  • Loading branch information
MichaelTiemannOSC and rhshadrach authored Nov 22, 2023
1 parent 18f7daf commit 4ee86a7
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,11 @@ def trans(x):
# if we don't have any elements, just astype it
return trans(result).astype(dtype)

# do a test on the first element, if it fails then we are done
r = result.ravel()
arr = np.array([r[0]])

if isna(arr).any():
# if we have any nulls, then we are done
return result

elif not isinstance(r[0], (np.integer, np.floating, int, float, bool)):
if isinstance(result, np.ndarray):
element = result.item(0)
else:
element = result.iloc[0]
if not isinstance(element, (np.integer, np.floating, int, float, bool)):
# a comparable, e.g. a Decimal may slip in here
return result

Expand Down

0 comments on commit 4ee86a7

Please sign in to comment.