8000 Merge pull request #25484 from ngoldbaum/fix-replace-scalar · numpy/numpy@a43c068 · GitHub
[go: up one dir, main page]

Skip to content

Commit a43c068

Browse files
authored
Merge pull request #25484 from ngoldbaum/fix-replace-scalar
BUG: handle scalar input in np.char.replace
2 parents 1b861a2 + 7848a0c commit a43c068

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

numpy/_core/defchararray.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,18 +1383,19 @@ def replace(a, old, new, count=None):
13831383
array(['The dwash was fresh', 'Thwas was it'], dtype='<U19')
13841384
13851385
"""
1386+
a_arr = numpy.asarray(a)
13861387
max_int64 = numpy.iinfo(numpy.int64).max
13871388
count = count if count is not None else max_int64
13881389

1389-
counts = numpy._core.umath.count(a, old, 0, max_int64)
1390+
counts = numpy._core.umath.count(a_arr, old, 0, max_int64)
13901391
buffersizes = (
1391-
numpy._core.umath.str_len(a)
1392+
numpy._core.umath.str_len(a_arr)
13921393
+ counts * (numpy._core.umath.str_len(new) -
13931394
numpy._core.umath.str_len(old))
13941395
)
13951396
max_buffersize = numpy.max(buffersizes)
1396-
out = numpy.empty(a.shape, dtype=f"{a.dtype.char}{max_buffersize}")
1397-
numpy._core.umath._replace(a, old, new, count, out=out)
1397+
out = numpy.empty(a_arr.shape, dtype=f"{a_arr.dtype.char}{max_buffersize}")
1398+
numpy._core.umath._replace(a_arr, old, new, count, out=out)
13981399
return out
13991400

14001401

0 commit comments

Comments
 (0)
0