@@ -746,6 +746,12 @@ def _reduce(
746
746
axis : AxisInt | None = 0 ,
747
747
** kwargs ,
748
748
):
749
+ if self .dtype .na_value is np .nan and name in ["any" , "all" ]:
750
+ if name == "any" :
751
+ return nanops .nanany (self ._ndarray , skipna = skipna )
752
+ else :
753
+ return nanops .nanall (self ._ndarray , skipna = skipna )
754
+
749
755
if name in ["min" , "max" ]:
750
756
result = getattr (self , name )(skipna = skipna , axis = axis )
751
757
if keepdims :
@@ -754,6 +760,12 @@ def _reduce(
754
760
755
761
raise TypeError (f"Cannot perform reduction '{ name } ' with string dtype" )
756
762
763
+ def _wrap_reduction_result (self , axis : AxisInt | None , result ) -> Any :
764
+ if self .dtype .na_value is np .nan and result is libmissing .NA :
765
+ # the masked_reductions use pd.NA -> convert to np.nan
766
+ return np .nan
767
+ return super ()._wrap_reduction_result (axis , result )
768
+
757
769
def min (self , axis = None , skipna : bool = True , ** kwargs ) -> Scalar :
758
770
nv .validate_min ((), kwargs )
759
771
result = masked_reductions .min (
@@ -771,8 +783,11 @@ def max(self, axis=None, skipna: bool = True, **kwargs) -> Scalar:
771
783
def value_counts (self , dropna : bool = True ) -> Series :
772
784
from pandas .core .algorithms import value_counts_internal as value_counts
773
785
774
- result = value_counts (self ._ndarray , sort = False , dropna = dropna ). astype ( "Int64" )
786
+ result = value_counts (self ._ndarray , sort = False , dropna = dropna )
775
787
result .index = result .index .astype (self .dtype )
788
+
789
+ if self .dtype .na_value is libmissing .NA :
790
+ result = result .astype ("Int64" )
776
791
return result
777
792
778
793
def memory_usage (self , deep : bool = False ) -> int :
@@ -823,7 +838,13 @@ def _cmp_method(self, other, op):
823
838
# logical
824
839
result = np .zeros (len (self ._ndarray ), dtype = "bool" )
825
840
result [valid ] = op (self ._ndarray [valid ], other )
826
- return BooleanArray (result , mask )
841
+ res_arr = BooleanArray (result , mask )
842
+ if self .dtype .na_value is np .nan :
843
+ if op == operator .ne :
844
+ return res_arr .to_numpy (np .bool_ , na_value = True )
845
+ else :
846
+ return res_arr .to_numpy (np .bool_ , na_value = False )
847
+ return res_arr
827
848
828
849
_arith_method = _cmp_method
829
850
@@ -864,37 +885,6 @@ def _from_backing_data(self, arr: np.ndarray) -> StringArrayNumpySemantics:
864
885
# we always preserve the dtype
865
886
return NDArrayBacked ._from_backing_data (self , arr )
866
887
867
- def _reduce (
868
- self , name : str , * , skipna : bool = True , keepdims : bool = False , ** kwargs
869
- ):
870
- if name in ["any" , "all" ]:
871
- if name == "any" :
872
- return nanops .nanany (self ._ndarray , skipna = skipna )
873
- else :
874
- return nanops .nanall (self ._ndarray , skipna = skipna )
875
- else :
876
- return super ()._reduce (name , skipna = skipna , keepdims = keepdims , ** kwargs )
877
-
878
- def _wrap_reduction_result (self , axis : AxisInt | None , result ) -> Any :
879
- # the masked_reductions use pd.NA
880
- if result is libmissing .NA :
881
- return np .nan
882
- return super ()._wrap_reduction_result (axis , result )
883
-
884
- def _cmp_method (self , other , op ):
885
- result = super ()._cmp_method (other , op )
886
- if op == operator .ne :
887
- return result .to_numpy (np .bool_ , na_value = True )
888
- else :
889
- return result .to_numpy (np .bool_ , na_value = False )
890
-
891
- def value_counts (self , dropna : bool = True ) -> Series :
892
- from pandas .core .algorithms import value_counts_internal as value_counts
893
-
894
- result = value_counts (self ._ndarray , sort = False , dropna = dropna )
895
- result .index = result .index .astype (self .dtype )
896
- return result
897
-
898
888
# ------------------------------------------------------------------------
899
889
# String methods interface
900
890
_str_na_value = np .nan
0 commit comments