-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ArrowExtensionArray to proper NumPy dtype (#56290)
- Loading branch information
Showing
6 changed files
with
89 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
|
||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
) | ||
|
||
import numpy as np | ||
|
||
from pandas._libs import lib | ||
from pandas.errors import LossySetitemError | ||
|
||
from pandas.core.dtypes.cast import np_can_hold_element | ||
from pandas.core.dtypes.common import is_numeric_dtype | ||
|
||
if TYPE_CHECKING: | ||
from pandas._typing import ( | ||
ArrayLike, | ||
npt, | ||
) | ||
|
||
|
||
def to_numpy_dtype_inference( | ||
arr: ArrayLike, dtype: npt.DTypeLike | None, na_value, hasna: bool | ||
) -> tuple[npt.DTypeLike, Any]: | ||
if dtype is None and is_numeric_dtype(arr.dtype): | ||
dtype_given = False | ||
if hasna: | ||
if arr.dtype.kind == "b": | ||
dtype = np.dtype(np.object_) | ||
else: | ||
if arr.dtype.kind in "iu": | ||
dtype = np.dtype(np.float64) | ||
else: | ||
dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] | ||
if na_value is lib.no_default: | ||
na_value = np.nan | ||
else: | ||
dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] | ||
elif dtype is not None: | ||
dtype = np.dtype(dtype) | ||
dtype_given = True | ||
else: | ||
dtype_given = True | ||
|
||
if na_value is lib.no_default: | ||
na_value = arr.dtype.na_value | ||
|
||
if not dtype_given and hasna: | ||
try: | ||
np_can_hold_element(dtype, na_value) # type: ignore[arg-type] | ||
except LossySetitemError: | ||
dtype = np.dtype(np.object_) | ||
return dtype, na_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters