10000 Fix np.char for scalars and add tests · numpy/numpy@a7a97d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a7a97d1

Browse files
committed
Fix np.char for scalars and add tests
1 parent c9340c2 commit a7a97d1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

numpy/_core/defchararray.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def _to_bytes_or_str_array(result, output_dtype_like):
6262
with the appropriate dtype if an object array must be used
6363
as an intermediary.
6464
"""
65+
output_dtype_like = numpy.asarray(output_dtype_like)
6566
if result.size == 0:
6667
# Calling asarray & tolist in an empty array would result
6768
# in losing shape information

numpy/_core/tests/test_defchararray.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,51 @@ def test_decode_with_reshape(self):
692692
assert_(res.shape == (1, 0, 1))
693693

694694

695+
class TestMethodsScalarValues:
696+
def test_mod(self):
697+
A = np.array([[' abc ', ''],
698+
['12345', 'MixedCase'],
699+
['123 \t 345 \0 ', 'UPPER']], dtype='S')
700+
tgt = [[b'123 abc ', b'123'],
701+
[b'12312345', b'123MixedCase'],
702+
[b'123123 \t 345 \0 ', b'123UPPER']]
703+
assert_array_equal(np.char.mod(b"123%s", A), tgt)
704+
705+
def test_decode(self):
706+
bytestring = b'\x81\xc1\x81\xc1\x81\xc1'
707+
assert_equal(np.char.decode(bytestring, encoding='cp037'),
708+
'aAaAaA')
709+
710+
def test_encode(self):
711+
unicode = 'aAaAaA'
712+
assert_equal(np.char.encode(unicode, encoding='cp037'),
713+
b'\x81\xc1\x81\xc1\x81\xc1')
714+
715+
def test_expandtabs(self):
716+
s = "\tone level of indentation\n\t\ttwo levels of indentation"
717+
assert_equal(
718+
np.char.expandtabs(s, tabsize=2),
719+
" one level of indentation\n two levels of indentation"
720+
)
721+
722+
def test_join(self):
723+
seps = np.array(['-', '_'])
724+
assert_array_equal(np.char.join(seps, 'hello'),
725+
['h-e-l-l-o', 'h_e_l_l_o'])
726+
727+
def test_partition(self):
728+
assert_equal(np.char.partition('This string', ' '),
729+
['This', ' ', 'string'])
730+
731+
def test_rpartition(self):
732+
assert_equal(np.char.rpartition('This string here', ' '),
733+
['This string', ' ', 'here'])
734+
735+
def test_replace(self):
736+
assert_equal(np.char.replace('Python is good', 'good', 'great'),
737+
'Python is great')
738+
739+
695740
def test_empty_indexing():
696741
"""Regression test for ticket 1948."""
697742
# Check that indexing a chararray with an empty list/array returns an

0 commit comments

Comments
 (0)
0