Skip to content

Commit 4bc49ed

Browse files
committed
fixed comparison of string column to mixed object column (issue pandas-dev#60228)
1 parent 72ab3fd commit 4bc49ed

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pandas/core/ops/array_ops.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
is_bool_dtype,
3939
is_list_like,
4040
is_numeric_v_string_like,
41+
is_string_dtype,
4142
is_object_dtype,
4243
is_scalar,
4344
)
@@ -53,7 +54,7 @@
5354

5455
from pandas.core import roperator
5556
from pandas.core.computation import expressions
56-
from pandas.core.construction import ensure_wrapped_if_datetimelike
57+
from pandas.core.construction import ensure_wrapped_if_datetimelike, array
5758
from pandas.core.ops import missing
5859
from pandas.core.ops.dispatch import should_extension_dispatch
5960
from pandas.core.ops.invalid import invalid_comparison
@@ -321,6 +322,17 @@ def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
321322
"Lengths must match to compare", lvalues.shape, rvalues.shape
322323
)
323324

325+
if (
326+
(is_string_dtype(lvalues) and is_object_dtype(rvalues)) or
327+
(is_object_dtype(lvalues) and is_string_dtype(rvalues))
328+
):
329+
if lvalues.dtype.name == "string" and rvalues.dtype == object:
330+
lvalues = lvalues.astype("string")
331+
rvalues = array(rvalues, dtype="string")
332+
elif rvalues.dtype.name == "string" and lvalues.dtype == object:
333+
rvalues = rvalues.astype("string")
334+
lvalues = array(lvalues, dtype="string")
335+
324336
if should_extension_dispatch(lvalues, rvalues) or (
325337
(isinstance(rvalues, (Timedelta, BaseOffset, Timestamp)) or right is NaT)
326338
and lvalues.dtype != object

0 commit comments

Comments
 (0)