@@ -1690,7 +1690,10 @@ def map_array(
1690
1690
else :
1691
1691
mapper = Series (mapper , dtype = np .float64 )
1692
1692
else :
1693
- mapper = Series (mapper )
1693
+ if arr .dtype in ("string[pyarrow]" , "string[python]" ):
1694
+ mapper = Series (mapper , dtype = arr .dtype )
1695
+ else :
1696
+ mapper = Series (mapper )
1694
1697
1695
1698
if isinstance (mapper , ABCSeries ):
1696
1699
if na_action == "ignore" :
@@ -1706,33 +1709,7 @@ def map_array(
1706
1709
if not len (arr ):
1707
1710
return arr .copy ()
1708
1711
1709
- na_value = np .nan
1710
- mask = isna (arr )
1711
- storage = None
1712
- if isinstance (arr .dtype , BaseMaskedDtype ):
1713
- arr = cast ("BaseMaskedArray" , arr )
1714
- values = arr ._data
1715
- if arr ._hasna :
1716
- na_value = arr .dtype .na_value
1717
- elif isinstance (arr .dtype , ExtensionDtype ):
1718
- arr = cast ("ExtensionArray" , arr )
1719
- arr_dtype = arr .dtype .__repr__ ()
1720
- if "python" in arr_dtype :
1721
- storage = "python"
1722
- values = np .asarray (arr )
1723
- elif "pyarrow" in arr_dtype :
1724
- storage = "pyarrow"
1725
- if "date" in arr_dtype :
1726
- values = np .fromiter (arr ._pa_array , dtype = "O" )
1727
- else :
1728
- values = np .asarray (arr )
1729
- else :
1730
- values = np .asarray (arr )
1731
- if arr ._hasna :
1732
- na_value = arr .dtype .na_value
1733
- else :
1734
- # we must convert to python types
1735
- values = arr .astype (object , copy = False )
1712
+ mask , na_value , storage , values = _build_map_infer_methods_params (arr )
1736
1713
1737
1714
if na_action is None :
1738
1715
return lib .map_infer (
@@ -1752,3 +1729,49 @@ def map_array(
1752
1729
convert_to_nullable_dtype = na_value is NA ,
1753
1730
storage = storage ,
1754
1731
)
1732
+
1733
+
1734
+ def _build_map_infer_methods_params (arr : ArrayLike ):
1735
+ """
1736
+ Process lib.map_infer and lib.map_infer_mask parameters from an array `arr`
1737
+
1738
+ Parameters
1739
+ ----------
1740
+ arr
1741
+
1742
+ Returns
1743
+ -------
1744
+ mask : np.ndarray[bool]
1745
+ na_value : object
1746
+ A value in `values` to consider missing.
1747
+ storage : {"python", "pyarrow", "pyarrow_numpy"}, default "python"
1748
+ Backend storage
1749
+ values : np.ndarray
1750
+ Values to be processed by lib.map_infer and lib.map_infer_mask
1751
+
1752
+ """
1753
+ na_value = np .nan
1754
+ mask = isna (arr )
1755
+ storage = "python"
1756
+ if isinstance (arr .dtype , BaseMaskedDtype ):
1757
+ arr = cast ("BaseMaskedArray" , arr )
1758
+ values = arr ._data
1759
+ if arr ._hasna :
1760
+ na_value = arr .dtype .na_value
1761
+
1762
+ elif isinstance (arr .dtype , ExtensionDtype ):
1763
+ arr = cast ("ExtensionArray" , arr )
1764
+ arr_dtype = arr .dtype .__repr__ ()
1765
+ if "pyarrow" in arr_dtype and "date" in arr_dtype :
1766
+ values = np .fromiter (arr ._pa_array , dtype = "O" )
1767
+ else :
1768
+ values = np .asarray (arr )
1769
+ if "pyarrow" in arr_dtype :
1770
+ storage = "pyarrow"
1771
+ if arr ._hasna :
1772
+ na_value = arr .dtype .na_value
1773
+
1774
+ else :
1775
+ # we must convert to python types
1776
+ values = arr .astype (object , copy = False )
1777
+ return mask , na_value , storage , values
0 commit comments