Skip to content

Commit 0cfd0c5

Browse files
authored
Merge pull request numpy#26066 from seberg/fix-string-sum
BUG: Allow the new string dtype summation to work
2 parents 5bd85c7 + 8ad2ef9 commit 0cfd0c5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

numpy/_core/src/umath/ufunc_object.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,13 @@ reducelike_promote_and_resolve(PyUFuncObject *ufunc,
24072407
out_descrs[0], out_descrs[1], out_descrs[2]);
24082408
goto fail;
24092409
}
2410+
/*
2411+
* After checking that they are equivalent, we enforce the use of the out
2412+
* one (which the user should have defined). (Needed by string dtype)
2413+
*/
2414+
Py_INCREF(out_descrs[2]);
2415+
Py_SETREF(out_descrs[0], out_descrs[2]);
2416+
24102417
/* TODO: This really should _not_ be unsafe casting (same above)! */
24112418
if (validate_casting(ufuncimpl, ufunc, ops, out_descrs, casting) < 0) {
24122419
goto fail;

numpy/_core/tests/test_stringdtype.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,18 @@ def test_ufunc_add(dtype, string_list, other_strings, use_out):
718718
np.add(arr1, arr2)
719719

720720

721+
def test_ufunc_add_reduce(dtype):
722+
values = ["a", "this is a long string", "c"]
723+
arr = np.array(values, dtype=dtype)
724+
out = np.empty((), dtype=dtype)
725+
726+
expected = np.array("".join(values), dtype=dtype)
727+
assert_array_equal(np.add.reduce(arr), expected)
728+
729+
np.add.reduce(arr, out=out)
730+
assert_array_equal(out, expected)
731+
732+
721733
def test_add_promoter(string_list):
722734
arr = np.array(string_list, dtype=StringDType())
723735
lresult = np.array(["hello" + s for s in string_list], dtype=StringDType())

0 commit comments

Comments
 (0)