Skip to content

Commit b7d4aa1

Browse files
Ethan0456charris
authored andcommitted
BUG: fix StringDType coerce flag in binary ufunc promotion (#31861) (#31862)
1 parent 7bcebec commit b7d4aa1

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
@@ -1820,10 +1820,13 @@ def test_unset_na_coercion():
18201820
StringDType(na_object=None)]:
18211821
if op_dtype is None:
18221822
op = "2"
1823+
expected = StringDType(na_object=None) # coerce defaults to True
18231824
else:
18241825
op = np.array("2", dtype=op_dtype)
1826+
expected = StringDType(na_object=None, coerce=op_dtype.coerce)
18251827
res = arr + op
18261828
assert_array_equal(res, ["hello2", "world2"])
1829+
assert res.dtype == expected
18271830

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

0 commit comments

Comments
 (0)