@@ -428,6 +428,45 @@ def _str_map_str_or_object(
428428 # -> We don't know the result type. E.g. `.get` can return anything.
429429 return lib .map_infer_mask (arr , f , mask .view ("uint8" ))
430430
431+ def _str_map_nan_semantics (
432+ self , f , na_value = None , dtype : Dtype | None = None , convert : bool = True
433+ ):
434+ if dtype is None :
435+ dtype = self .dtype
436+ if na_value is None :
437+ na_value = self .dtype .na_value
438+
439+ mask = isna (self )
440+ arr = np .asarray (self )
441+ convert = convert and not np .all (mask )
442+
443+ if is_integer_dtype (dtype ) or is_bool_dtype (dtype ):
444+ na_value_is_na = isna (na_value )
445+ if na_value_is_na :
446+ if is_integer_dtype (dtype ):
447+ na_value = 0
448+ else :
449+ na_value = True
450+
451+ result = lib .map_infer_mask (
452+ arr ,
453+ f ,
454+ mask .view ("uint8" ),
455+ convert = False ,
456+ na_value = na_value ,
457+ dtype = np .dtype (cast (type , dtype )),
458+ )
459+ if na_value_is_na and mask .any ():
460+ if is_integer_dtype (dtype ):
461+ result = result .astype ("float64" )
462+ else :
463+ result = result .astype ("object" )
464+ result [mask ] = np .nan
465+ return result
466+
467+ else :
468+ return self ._str_map_str_or_object (dtype , na_value , arr , f , mask , convert )
469+
431470
432471# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
433472# incompatible with definition in base class "ExtensionArray"
@@ -793,45 +832,6 @@ def _cmp_method(self, other, op):
793832 # base class "NumpyExtensionArray" defined the type as "float")
794833 _str_na_value = libmissing .NA # type: ignore[assignment]
795834
796- def _str_map_nan_semantics (
797- self , f , na_value = None , dtype : Dtype | None = None , convert : bool = True
798- ):
799- if dtype is None :
800- dtype = self .dtype
801- if na_value is None :
802- na_value = self .dtype .na_value
803-
804- mask = isna (self )
805- arr = np .asarray (self )
806- convert = convert and not np .all (mask )
807-
808- if is_integer_dtype (dtype ) or is_bool_dtype (dtype ):
809- na_value_is_na = isna (na_value )
810- if na_value_is_na :
811- if is_integer_dtype (dtype ):
812- na_value = 0
813- else :
814- na_value = True
815-
816- result = lib .map_infer_mask (
817- arr ,
818- f ,
819- mask .view ("uint8" ),
820- convert = False ,
821- na_value = na_value ,
822- dtype = np .dtype (cast (type , dtype )),
823- )
824- if na_value_is_na and mask .any ():
825- if is_integer_dtype (dtype ):
826- result = result .astype ("float64" )
827- else :
828- result = result .astype ("object" )
829- result [mask ] = np .nan
830- return result
831-
832- else :
833- return self ._str_map_str_or_object (dtype , na_value , arr , f , mask , convert )
834-
835835
836836class StringArrayNumpySemantics (StringArray ):
837837 _storage = "python"
0 commit comments