Skip to content

Commit 73c59e7

Browse files
Ethan0456ngoldbaum
authored andcommitted
BUG: fix StringDType coerce flag in binary ufunc promotion (numpy#31861) (numpy#31862)
(cherry picked from commit 5075498)
1 parent ff90f1a commit 73c59e7

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

‎numpy/_core/src/umath/stringdtype_ufuncs.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ binary_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method),
248248
{
249249
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0];
250250
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1];
251-
int out_coerce = descr1->coerce && descr1->coerce;
251+
int out_coerce = descr1->coerce && descr2->coerce;
252252
PyObject *out_na_object = NULL;
253253

254254
if (stringdtype_compatible_na(

‎numpy/_core/tests/test_stringdtype.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,10 +2143,13 @@ def test_unset_na_coercion():
21432143
StringDType(na_object=None)]:
21442144
if op_dtype is None:
21452145
op = "2"
2146+
expected = StringDType(na_object=None) # coerce defaults to True
21462147
else:
21472148
op = np.array("2", dtype=op_dtype)
2149+
expected = StringDType(na_object=None, coerce=op_dtype.coerce)
21482150
res = arr + op
21492151
assert_array_equal(res, ["hello2", "world2"])
2152+
assert res.dtype == expected
21502153

21512154
# dtype instances with distinct explicitly set NA objects are incompatible
21522155
for op_dtype in [StringDType(na_object=pd_NA), StringDType(na_object="")]:

0 commit comments

Comments
 (0)