@@ -134,6 +134,10 @@ class StringArray(PandasArray):
134
134
The string methods are available on Series backed by
135
135
a StringArray.
136
136
137
+ Notes
138
+ -----
139
+ StringArray returns a BooleanArray for comparison methods.
140
+
137
141
Examples
138
142
--------
139
143
>>> pd.array(['This is', 'some text', None, 'data.'], dtype="string")
@@ -148,6 +152,13 @@ class StringArray(PandasArray):
148
152
Traceback (most recent call last):
149
153
...
150
154
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
151
162
"""
152
163
153
164
# undo the PandasArray hack
@@ -255,7 +266,12 @@ def value_counts(self, dropna=False):
255
266
# Overrride parent because we have different return types.
256
267
@classmethod
257
268
def _create_arithmetic_method (cls , op ):
269
+ # Note: this handles both arithmetic and comparison methods.
258
270
def method (self , other ):
271
+ from pandas .arrays import BooleanArray
272
+
273
+ assert op .__name__ in ops .ARITHMETIC_BINOPS | ops .COMPARISON_BINOPS
274
+
259
275
if isinstance (other , (ABCIndexClass , ABCSeries , ABCDataFrame )):
260
276
return NotImplemented
261
277
@@ -275,15 +291,16 @@ def method(self, other):
275
291
other = np .asarray (other )
276
292
other = other [valid ]
277
293
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 )
283
298
return StringArray (result )
284
299
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 )
287
304
288
305
return compat .set_function_name (method , f"__{ op .__name__ } __" , cls )
289
306
0 commit comments