@@ -134,6 +134,10 @@ class StringArray(PandasArray):
134134 The string methods are available on Series backed by
135135 a StringArray.
136136
137+ Notes
138+ -----
139+ StringArray returns a BooleanArray for comparison methods.
140+
137141 Examples
138142 --------
139143 >>> pd.array(['This is', 'some text', None, 'data.'], dtype="string")
@@ -148,6 +152,13 @@ class StringArray(PandasArray):
148152 Traceback (most recent call last):
149153 ...
150154 ValueError: StringArray requires an object-dtype ndarray of strings.
155+
156+ For comparision methods, this returns a :class:`pandas.BooleanArray`
157+
158+ >>> pd.array(["a", None, "c"], dtype="string") == "a"
159+ <BooleanArray>
160+ [True, NA, False]
161+ Length: 3, dtype: boolean
151162 """
152163
153164 # undo the PandasArray hack
@@ -255,7 +266,12 @@ def value_counts(self, dropna=False):
255266 # Overrride parent because we have different return types.
256267 @classmethod
257268 def _create_arithmetic_method (cls , op ):
269+ # Note: this handles both arithmetic and comparison methods.
258270 def method (self , other ):
271+ from pandas .arrays import BooleanArray
272+
273+ assert op .__name__ in ops .ARITHMETIC_BINOPS | ops .COMPARISON_BINOPS
274+
259275 if isinstance (other , (ABCIndexClass , ABCSeries , ABCDataFrame )):
260276 return NotImplemented
261277
@@ -275,15 +291,16 @@ def method(self, other):
275291 other = np .asarray (other )
276292 other = other [valid ]
277293
278- result = np .empty_like (self ._ndarray , dtype = "object" )
279- result [mask ] = StringDtype .na_value
280- result [valid ] = op (self ._ndarray [valid ], other )
281-
282- if op .__name__ in {"add" , "radd" , "mul" , "rmul" }:
294+ if op .__name__ in ops .ARITHMETIC_BINOPS :
295+ result = np .empty_like (self ._ndarray , dtype = "object" )
296+ result [mask ] = StringDtype .na_value
297+ result [valid ] = op (self ._ndarray [valid ], other )
283298 return StringArray (result )
284299 else :
285- dtype = "object" if mask .any () else "bool"
286- return np .asarray (result , dtype = dtype )
300+ # logical
301+ result = np .zeros (len (self ._ndarray ), dtype = "bool" )
302+ result [valid ] = op (self ._ndarray [valid ], other )
303+ return BooleanArray (result , mask )
287304
288305 return compat .set_function_name (method , f"__{ op .__name__ } __" , cls )
289306
0 commit comments