8000 BUG: ensure np.vectorize doesn't truncate fixed-width strings · numpy/numpy@9796349 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9796349

Browse files
committed
BUG: ensure np.vectorize doesn't truncate fixed-width strings
1 parent de8aee4 commit 9796349

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -2614,3 +2614,9 @@ def test_logspace_base_does_not_determine_dtype(self):
26142614
base=np.array([10.0]))
26152615
with pytest.raises(AssertionError, match="not almost equal"):
26162616
assert_almost_equal(out2, expected)
2617+
2618+
def test_vectorize_fixed_width_string(self):
2619+
arr = np.array(["SOme wOrd DŽ ß ᾛ ΣΣ ffi⁵Å Ç Ⅰ"]).astype(np.str_)
2620+
f = str.casefold
2621+
res = np.vectorize(f, otypes=[arr.dtype])(arr)
2622+
assert res.dtype == "U30"
Original file line numberDiff line numberDiff line change
@@ -2132,6 +2132,12 @@ def _create_arrays(broadcast_shape, dim_sizes, list_of_core_dims, dtypes,
21322132
return arrays
21332133

21342134

2135+
def _get_vectorize_dtype(dtype):
2136+
if dtype.char in "SU":
2137+
return dtype.char
2138+
return dtype
2139+
2140+
21352141
@set_module('numpy')
21362142
class vectorize:
21372143
"""
@@ -2330,7 +2336,7 @@ def __init__(self, pyfunc=np._NoValue, otypes=None, doc=None,
23302336
if char not in typecodes['All']:
23312337
raise ValueError("Invalid otype specified: %s" % (char,))
23322338
elif iterable(otypes):
2333-
otypes = [_nx.dtype(x) for x in otypes]
2339+
otypes = [_get_vectorize_dtype(_nx.dtype(x)) for x in otypes]
23342340
elif otypes is not None:
23352341
raise ValueError("Invalid otype specification")
23362342
self.otypes = otypes