Skip to content

Commit 1ab81c0

Browse files
committed
Code simplification
1 parent ba7d37d commit 1ab81c0

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

pandas/core/algorithms.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
Series,
100100
)
101101
from pandas.core.arrays import (
102+
ArrowExtensionArray,
102103
BaseMaskedArray,
103104
ExtensionArray,
104105
)
@@ -1734,35 +1735,33 @@ def _build_map_infer_methods_params(arr: ArrayLike):
17341735
Values to be processed by lib.map_infer and lib.map_infer_mask
17351736
17361737
"""
1737-
na_value = np.nan
1738+
na_value = None
17381739
mask = isna(arr)
17391740
storage = None
1741+
if (
1742+
isinstance(arr.dtype, (BaseMaskedDtype, ExtensionDtype))
1743+
and hasattr(arr, "_hasna")
1744+
and arr._hasna
1745+
):
1746+
na_value = arr.dtype.na_value
1747+
17401748
if isinstance(arr.dtype, BaseMaskedDtype):
17411749
arr = cast("BaseMaskedArray", arr)
17421750
values = np.fromiter(arr._data, dtype="O")
1743-
if arr._hasna:
1744-
na_value = arr.dtype.na_value
17451751

17461752
elif isinstance(arr.dtype, ExtensionDtype):
1747-
from pandas.core.arrays.string_ import StringDtype
1748-
17491753
arr = cast("ExtensionArray", arr)
1750-
arr_dtype = arr.dtype.__repr__()
1751-
if (
1752-
isinstance(arr.dtype, StringDtype) and arr.dtype.storage == "pyarrow"
1753-
) or "pyarrow" in arr_dtype:
1754-
storage = "pyarrow"
1754+
if hasattr(arr.dtype, "storage"):
1755+
storage = arr.dtype.storage
1756+
1757+
if storage == "pyarrow":
1758+
arr = cast("ArrowExtensionArray", arr)
17551759
values = np.fromiter(arr._pa_array, dtype="O")
17561760
else:
17571761
values = np.asarray(arr)
1758-
if (
1759-
isinstance(arr.dtype, StringDtype) and arr.dtype.storage == "python"
1760-
) or "python" in arr_dtype:
1761-
storage = "python"
1762-
if arr._hasna:
1763-
na_value = arr.dtype.na_value
17641762

17651763
else:
17661764
# we must convert to python types
17671765
values = arr.astype(object, copy=False)
1766+
na_value = np.nan
17681767
return mask, na_value, storage, values

0 commit comments

Comments
 (0)