Skip to content

Commit 778af02

Browse files
committed
BUG: fix string type inconsistency between zeros and zeros_like
np.zeros for strings returns empty strings while np.zeros_like of a string array creates strings containing an string 0.
1 parent c09d0ce commit 778af02

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

doc/release/1.9.0-notes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ a problem cannot occur.
240240

241241
This change was already applied to the 1.8.1 release.
242242

243+
``zeros_like`` for string dtypes now returns empty strings
244+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245+
To match the `zeros` function `zeros_like` now returns an array initialized
246+
with empty strings instead of an array filled with `'0'`.
247+
243248

244249
New Features
245250
============

numpy/core/numeric.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def zeros_like(a, dtype=None, order='K', subok=True):
131131
132132
"""
133133
res = empty_like(a, dtype=dtype, order=order, subok=subok)
134-
multiarray.copyto(res, 0, casting='unsafe')
134+
# needed instead of a 0 to get same result as zeros for for string dtypes
135+
z = zeros(1, dtype=res.dtype)
136+
multiarray.copyto(res, z, casting='unsafe')
135137
return res
136138

137139
def ones(shape, dtype=None, order='C'):

numpy/core/tests/test_multiarray.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,40 @@ def test_zeros_obj_obj(self):
515515
d = zeros(10, dtype=[('k', object, 2)])
516516
assert_array_equal(d['k'], 0)
517517

518+
def test_zeros_like_like_zeros(self):
519+
# test zeros_like returns the same as zeros
520+
for c in np.typecodes['All']:
521+
if c == 'V':
522+
continue
523+
d = zeros((3,3), dtype=c)
524+
assert_array_equal(zeros_like(d), d)
525+
assert_equal(zeros_like(d).dtype, d.dtype)
526+
# explicitly check some special cases
527+
d = zeros((3,3), dtype='S5')
528+
assert_array_equal(zeros_like(d), d)
529+
assert_equal(zeros_like(d).dtype, d.dtype)
530+
d = zeros((3,3), dtype='U5')
531+
assert_array_equal(zeros_like(d), d)
532+
assert_equal(zeros_like(d).dtype, d.dtype)
533+
534+
d = zeros((3,3), dtype='<i4')
535+
assert_array_equal(zeros_like(d), d)
536+
assert_equal(zeros_like(d).dtype, d.dtype)
537+
d = zeros((3,3), dtype='>i4')
538+
assert_array_equal(zeros_like(d), d)
539+
assert_equal(zeros_like(d).dtype, d.dtype)
540+
541+
d = zeros((3,3), dtype='<M8[s]')
542+
assert_array_equal(zeros_like(d), d)
543+
assert_equal(zeros_like(d).dtype, d.dtype)
544+
d = zeros((3,3), dtype='>M8[s]')
545+
assert_array_equal(zeros_like(d), d)
546+
assert_equal(zeros_like(d).dtype, d.dtype)
547+
548+
d = zeros((3,3), dtype='f4,f4')
549+
assert_array_equal(zeros_like(d), d)
550+
assert_equal(zeros_like(d).dtype, d.dtype)
551+
518552
def test_sequence_non_homogenous(self):
519553
assert_equal(np.array([4, 2**80]).dtype, np.object)
520554
assert_equal(np.array([4, 2**80, 4]).dtype, np.object)

0 commit comments

Comments
 (0)