test_defchararray.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. import numpy as np
  2. from numpy.core.multiarray import _vec_string
  3. from numpy.testing import (
  4. assert_, assert_equal, assert_array_equal, assert_raises,
  5. assert_raises_regex
  6. )
  7. kw_unicode_true = {'unicode': True} # make 2to3 work properly
  8. kw_unicode_false = {'unicode': False}
  9. class TestBasic:
  10. def test_from_object_array(self):
  11. A = np.array([['abc', 2],
  12. ['long ', '0123456789']], dtype='O')
  13. B = np.char.array(A)
  14. assert_equal(B.dtype.itemsize, 10)
  15. assert_array_equal(B, [[b'abc', b'2'],
  16. [b'long', b'0123456789']])
  17. def test_from_object_array_unicode(self):
  18. A = np.array([['abc', 'Sigma \u03a3'],
  19. ['long ', '0123456789']], dtype='O')
  20. assert_raises(ValueError, np.char.array, (A,))
  21. B = np.char.array(A, **kw_unicode_true)
  22. assert_equal(B.dtype.itemsize, 10 * np.array('a', 'U').dtype.itemsize)
  23. assert_array_equal(B, [['abc', 'Sigma \u03a3'],
  24. ['long', '0123456789']])
  25. def test_from_string_array(self):
  26. A = np.array([[b'abc', b'foo'],
  27. [b'long ', b'0123456789']])
  28. assert_equal(A.dtype.type, np.string_)
  29. B = np.char.array(A)
  30. assert_array_equal(B, A)
  31. assert_equal(B.dtype, A.dtype)
  32. assert_equal(B.shape, A.shape)
  33. B[0, 0] = 'changed'
  34. assert_(B[0, 0] != A[0, 0])
  35. C = np.char.asarray(A)
  36. assert_array_equal(C, A)
  37. assert_equal(C.dtype, A.dtype)
  38. C[0, 0] = 'changed again'
  39. assert_(C[0, 0] != B[0, 0])
  40. assert_(C[0, 0] == A[0, 0])
  41. def test_from_unicode_array(self):
  42. A = np.array([['abc', 'Sigma \u03a3'],
  43. ['long ', '0123456789']])
  44. assert_equal(A.dtype.type, np.unicode_)
  45. B = np.char.array(A)
  46. assert_array_equal(B, A)
  47. assert_equal(B.dtype, A.dtype)
  48. assert_equal(B.shape, A.shape)
  49. B = np.char.array(A, **kw_unicode_true)
  50. assert_array_equal(B, A)
  51. assert_equal(B.dtype, A.dtype)
  52. assert_equal(B.shape, A.shape)
  53. def fail():
  54. np.char.array(A, **kw_unicode_false)
  55. assert_raises(UnicodeEncodeError, fail)
  56. def test_unicode_upconvert(self):
  57. A = np.char.array(['abc'])
  58. B = np.char.array(['\u03a3'])
  59. assert_(issubclass((A + B).dtype.type, np.unicode_))
  60. def test_from_string(self):
  61. A = np.char.array(b'abc')
  62. assert_equal(len(A), 1)
  63. assert_equal(len(A[0]), 3)
  64. assert_(issubclass(A.dtype.type, np.string_))
  65. def test_from_unicode(self):
  66. A = np.char.array('\u03a3')
  67. assert_equal(len(A), 1)
  68. assert_equal(len(A[0]), 1)
  69. assert_equal(A.itemsize, 4)
  70. assert_(issubclass(A.dtype.type, np.unicode_))
  71. class TestVecString:
  72. def test_non_existent_method(self):
  73. def fail():
  74. _vec_string('a', np.string_, 'bogus')
  75. assert_raises(AttributeError, fail)
  76. def test_non_string_array(self):
  77. def fail():
  78. _vec_string(1, np.string_, 'strip')
  79. assert_raises(TypeError, fail)
  80. def test_invalid_args_tuple(self):
  81. def fail():
  82. _vec_string(['a'], np.string_, 'strip', 1)
  83. assert_raises(TypeError, fail)
  84. def test_invalid_type_descr(self):
  85. def fail():
  86. _vec_string(['a'], 'BOGUS', 'strip')
  87. assert_raises(TypeError, fail)
  88. def test_invalid_function_args(self):
  89. def fail():
  90. _vec_string(['a'], np.string_, 'strip', (1,))
  91. assert_raises(TypeError, fail)
  92. def test_invalid_result_type(self):
  93. def fail():
  94. _vec_string(['a'], np.int_, 'strip')
  95. assert_raises(TypeError, fail)
  96. def test_broadcast_error(self):
  97. def fail():
  98. _vec_string([['abc', 'def']], np.int_, 'find', (['a', 'd', 'j'],))
  99. assert_raises(ValueError, fail)
  100. class TestWhitespace:
  101. def setup_method(self):
  102. self.A = np.array([['abc ', '123 '],
  103. ['789 ', 'xyz ']]).view(np.chararray)
  104. self.B = np.array([['abc', '123'],
  105. ['789', 'xyz']]).view(np.chararray)
  106. def test1(self):
  107. assert_(np.all(self.A == self.B))
  108. assert_(np.all(self.A >= self.B))
  109. assert_(np.all(self.A <= self.B))
  110. assert_(not np.any(self.A > self.B))
  111. assert_(not np.any(self.A < self.B))
  112. assert_(not np.any(self.A != self.B))
  113. class TestChar:
  114. def setup_method(self):
  115. self.A = np.array('abc1', dtype='c').view(np.chararray)
  116. def test_it(self):
  117. assert_equal(self.A.shape, (4,))
  118. assert_equal(self.A.upper()[:2].tobytes(), b'AB')
  119. class TestComparisons:
  120. def setup_method(self):
  121. self.A = np.array([['abc', '123'],
  122. ['789', 'xyz']]).view(np.chararray)
  123. self.B = np.array([['efg', '123 '],
  124. ['051', 'tuv']]).view(np.chararray)
  125. def test_not_equal(self):
  126. assert_array_equal((self.A != self.B), [[True, False], [True, True]])
  127. def test_equal(self):
  128. assert_array_equal((self.A == self.B), [[False, True], [False, False]])
  129. def test_greater_equal(self):
  130. assert_array_equal((self.A >= self.B), [[False, True], [True, True]])
  131. def test_less_equal(self):
  132. assert_array_equal((self.A <= self.B), [[True, True], [False, False]])
  133. def test_greater(self):
  134. assert_array_equal((self.A > self.B), [[False, False], [True, True]])
  135. def test_less(self):
  136. assert_array_equal((self.A < self.B), [[True, False], [False, False]])
  137. def test_type(self):
  138. out1 = np.char.equal(self.A, self.B)
  139. out2 = np.char.equal('a', 'a')
  140. assert_(isinstance(out1, np.ndarray))
  141. assert_(isinstance(out2, np.ndarray))
  142. class TestComparisonsMixed1(TestComparisons):
  143. """Ticket #1276"""
  144. def setup_method(self):
  145. TestComparisons.setup_method(self)
  146. self.B = np.array([['efg', '123 '],
  147. ['051', 'tuv']], np.unicode_).view(np.chararray)
  148. class TestComparisonsMixed2(TestComparisons):
  149. """Ticket #1276"""
  150. def setup_method(self):
  151. TestComparisons.setup_method(self)
  152. self.A = np.array([['abc', '123'],
  153. ['789', 'xyz']], np.unicode_).view(np.chararray)
  154. class TestInformation:
  155. def setup_method(self):
  156. self.A = np.array([[' abc ', ''],
  157. ['12345', 'MixedCase'],
  158. ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
  159. self.B = np.array([[' \u03a3 ', ''],
  160. ['12345', 'MixedCase'],
  161. ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
  162. def test_len(self):
  163. assert_(issubclass(np.char.str_len(self.A).dtype.type, np.integer))
  164. assert_array_equal(np.char.str_len(self.A), [[5, 0], [5, 9], [12, 5]])
  165. assert_array_equal(np.char.str_len(self.B), [[3, 0], [5, 9], [12, 5]])
  166. def test_count(self):
  167. assert_(issubclass(self.A.count('').dtype.type, np.integer))
  168. assert_array_equal(self.A.count('a'), [[1, 0], [0, 1], [0, 0]])
  169. assert_array_equal(self.A.count('123'), [[0, 0], [1, 0], [1, 0]])
  170. # Python doesn't seem to like counting NULL characters
  171. # assert_array_equal(self.A.count('\0'), [[0, 0], [0, 0], [1, 0]])
  172. assert_array_equal(self.A.count('a', 0, 2), [[1, 0], [0, 0], [0, 0]])
  173. assert_array_equal(self.B.count('a'), [[0, 0], [0, 1], [0, 0]])
  174. assert_array_equal(self.B.count('123'), [[0, 0], [1, 0], [1, 0]])
  175. # assert_array_equal(self.B.count('\0'), [[0, 0], [0, 0], [1, 0]])
  176. def test_endswith(self):
  177. assert_(issubclass(self.A.endswith('').dtype.type, np.bool_))
  178. assert_array_equal(self.A.endswith(' '), [[1, 0], [0, 0], [1, 0]])
  179. assert_array_equal(self.A.endswith('3', 0, 3), [[0, 0], [1, 0], [1, 0]])
  180. def fail():
  181. self.A.endswith('3', 'fdjk')
  182. assert_raises(TypeError, fail)
  183. def test_find(self):
  184. assert_(issubclass(self.A.find('a').dtype.type, np.integer))
  185. assert_array_equal(self.A.find('a'), [[1, -1], [-1, 6], [-1, -1]])
  186. assert_array_equal(self.A.find('3'), [[-1, -1], [2, -1], [2, -1]])
  187. assert_array_equal(self.A.find('a', 0, 2), [[1, -1], [-1, -1], [-1, -1]])
  188. assert_array_equal(self.A.find(['1', 'P']), [[-1, -1], [0, -1], [0, 1]])
  189. def test_index(self):
  190. def fail():
  191. self.A.index('a')
  192. assert_raises(ValueError, fail)
  193. assert_(np.char.index('abcba', 'b') == 1)
  194. assert_(issubclass(np.char.index('abcba', 'b').dtype.type, np.integer))
  195. def test_isalnum(self):
  196. assert_(issubclass(self.A.isalnum().dtype.type, np.bool_))
  197. assert_array_equal(self.A.isalnum(), [[False, False], [True, True], [False, True]])
  198. def test_isalpha(self):
  199. assert_(issubclass(self.A.isalpha().dtype.type, np.bool_))
  200. assert_array_equal(self.A.isalpha(), [[False, False], [False, True], [False, True]])
  201. def test_isdigit(self):
  202. assert_(issubclass(self.A.isdigit().dtype.type, np.bool_))
  203. assert_array_equal(self.A.isdigit(), [[False, False], [True, False], [False, False]])
  204. def test_islower(self):
  205. assert_(issubclass(self.A.islower().dtype.type, np.bool_))
  206. assert_array_equal(self.A.islower(), [[True, False], [False, False], [False, False]])
  207. def test_isspace(self):
  208. assert_(issubclass(self.A.isspace().dtype.type, np.bool_))
  209. assert_array_equal(self.A.isspace(), [[False, False], [False, False], [False, False]])
  210. def test_istitle(self):
  211. assert_(issubclass(self.A.istitle().dtype.type, np.bool_))
  212. assert_array_equal(self.A.istitle(), [[False, False], [False, False], [False, False]])
  213. def test_isupper(self):
  214. assert_(issubclass(self.A.isupper().dtype.type, np.bool_))
  215. assert_array_equal(self.A.isupper(), [[False, False], [False, False], [False, True]])
  216. def test_rfind(self):
  217. assert_(issubclass(self.A.rfind('a').dtype.type, np.integer))
  218. assert_array_equal(self.A.rfind('a'), [[1, -1], [-1, 6], [-1, -1]])
  219. assert_array_equal(self.A.rfind('3'), [[-1, -1], [2, -1], [6, -1]])
  220. assert_array_equal(self.A.rfind('a', 0, 2), [[1, -1], [-1, -1], [-1, -1]])
  221. assert_array_equal(self.A.rfind(['1', 'P']), [[-1, -1], [0, -1], [0, 2]])
  222. def test_rindex(self):
  223. def fail():
  224. self.A.rindex('a')
  225. assert_raises(ValueError, fail)
  226. assert_(np.char.rindex('abcba', 'b') == 3)
  227. assert_(issubclass(np.char.rindex('abcba', 'b').dtype.type, np.integer))
  228. def test_startswith(self):
  229. assert_(issubclass(self.A.startswith('').dtype.type, np.bool_))
  230. assert_array_equal(self.A.startswith(' '), [[1, 0], [0, 0], [0, 0]])
  231. assert_array_equal(self.A.startswith('1', 0, 3), [[0, 0], [1, 0], [1, 0]])
  232. def fail():
  233. self.A.startswith('3', 'fdjk')
  234. assert_raises(TypeError, fail)
  235. class TestMethods:
  236. def setup_method(self):
  237. self.A = np.array([[' abc ', ''],
  238. ['12345', 'MixedCase'],
  239. ['123 \t 345 \0 ', 'UPPER']],
  240. dtype='S').view(np.chararray)
  241. self.B = np.array([[' \u03a3 ', ''],
  242. ['12345', 'MixedCase'],
  243. ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
  244. def test_capitalize(self):
  245. tgt = [[b' abc ', b''],
  246. [b'12345', b'Mixedcase'],
  247. [b'123 \t 345 \0 ', b'Upper']]
  248. assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
  249. assert_array_equal(self.A.capitalize(), tgt)
  250. tgt = [[' \u03c3 ', ''],
  251. ['12345', 'Mixedcase'],
  252. ['123 \t 345 \0 ', 'Upper']]
  253. assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
  254. assert_array_equal(self.B.capitalize(), tgt)
  255. def test_center(self):
  256. assert_(issubclass(self.A.center(10).dtype.type, np.string_))
  257. C = self.A.center([10, 20])
  258. assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
  259. C = self.A.center(20, b'#')
  260. assert_(np.all(C.startswith(b'#')))
  261. assert_(np.all(C.endswith(b'#')))
  262. C = np.char.center(b'FOO', [[10, 20], [15, 8]])
  263. tgt = [[b' FOO ', b' FOO '],
  264. [b' FOO ', b' FOO ']]
  265. assert_(issubclass(C.dtype.type, np.string_))
  266. assert_array_equal(C, tgt)
  267. def test_decode(self):
  268. A = np.char.array([b'\\u03a3'])
  269. assert_(A.decode('unicode-escape')[0] == '\u03a3')
  270. def test_encode(self):
  271. B = self.B.encode('unicode_escape')
  272. assert_(B[0][0] == str(' \\u03a3 ').encode('latin1'))
  273. def test_expandtabs(self):
  274. T = self.A.expandtabs()
  275. assert_(T[2, 0] == b'123 345 \0')
  276. def test_join(self):
  277. # NOTE: list(b'123') == [49, 50, 51]
  278. # so that b','.join(b'123') results to an error on Py3
  279. A0 = self.A.decode('ascii')
  280. A = np.char.join([',', '#'], A0)
  281. assert_(issubclass(A.dtype.type, np.unicode_))
  282. tgt = np.array([[' ,a,b,c, ', ''],
  283. ['1,2,3,4,5', 'M#i#x#e#d#C#a#s#e'],
  284. ['1,2,3, ,\t, ,3,4,5, ,\x00, ', 'U#P#P#E#R']])
  285. assert_array_equal(np.char.join([',', '#'], A0), tgt)
  286. def test_ljust(self):
  287. assert_(issubclass(self.A.ljust(10).dtype.type, np.string_))
  288. C = self.A.ljust([10, 20])
  289. assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
  290. C = self.A.ljust(20, b'#')
  291. assert_array_equal(C.startswith(b'#'), [
  292. [False, True], [False, False], [False, False]])
  293. assert_(np.all(C.endswith(b'#')))
  294. C = np.char.ljust(b'FOO', [[10, 20], [15, 8]])
  295. tgt = [[b'FOO ', b'FOO '],
  296. [b'FOO ', b'FOO ']]
  297. assert_(issubclass(C.dtype.type, np.string_))
  298. assert_array_equal(C, tgt)
  299. def test_lower(self):
  300. tgt = [[b' abc ', b''],
  301. [b'12345', b'mixedcase'],
  302. [b'123 \t 345 \0 ', b'upper']]
  303. assert_(issubclass(self.A.lower().dtype.type, np.string_))
  304. assert_array_equal(self.A.lower(), tgt)
  305. tgt = [[' \u03c3 ', ''],
  306. ['12345', 'mixedcase'],
  307. ['123 \t 345 \0 ', 'upper']]
  308. assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
  309. assert_array_equal(self.B.lower(), tgt)
  310. def test_lstrip(self):
  311. tgt = [[b'abc ', b''],
  312. [b'12345', b'MixedCase'],
  313. [b'123 \t 345 \0 ', b'UPPER']]
  314. assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
  315. assert_array_equal(self.A.lstrip(), tgt)
  316. tgt = [[b' abc', b''],
  317. [b'2345', b'ixedCase'],
  318. [b'23 \t 345 \x00', b'UPPER']]
  319. assert_array_equal(self.A.lstrip([b'1', b'M']), tgt)
  320. tgt = [['\u03a3 ', ''],
  321. ['12345', 'MixedCase'],
  322. ['123 \t 345 \0 ', 'UPPER']]
  323. assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
  324. assert_array_equal(self.B.lstrip(), tgt)
  325. def test_partition(self):
  326. P = self.A.partition([b'3', b'M'])
  327. tgt = [[(b' abc ', b'', b''), (b'', b'', b'')],
  328. [(b'12', b'3', b'45'), (b'', b'M', b'ixedCase')],
  329. [(b'12', b'3', b' \t 345 \0 '), (b'UPPER', b'', b'')]]
  330. assert_(issubclass(P.dtype.type, np.string_))
  331. assert_array_equal(P, tgt)
  332. def test_replace(self):
  333. R = self.A.replace([b'3', b'a'],
  334. [b'##########', b'@'])
  335. tgt = [[b' abc ', b''],
  336. [b'12##########45', b'MixedC@se'],
  337. [b'12########## \t ##########45 \x00', b'UPPER']]
  338. assert_(issubclass(R.dtype.type, np.string_))
  339. assert_array_equal(R, tgt)
  340. def test_rjust(self):
  341. assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))
  342. C = self.A.rjust([10, 20])
  343. assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
  344. C = self.A.rjust(20, b'#')
  345. assert_(np.all(C.startswith(b'#')))
  346. assert_array_equal(C.endswith(b'#'),
  347. [[False, True], [False, False], [False, False]])
  348. C = np.char.rjust(b'FOO', [[10, 20], [15, 8]])
  349. tgt = [[b' FOO', b' FOO'],
  350. [b' FOO', b' FOO']]
  351. assert_(issubclass(C.dtype.type, np.string_))
  352. assert_array_equal(C, tgt)
  353. def test_rpartition(self):
  354. P = self.A.rpartition([b'3', b'M'])
  355. tgt = [[(b'', b'', b' abc '), (b'', b'', b'')],
  356. [(b'12', b'3', b'45'), (b'', b'M', b'ixedCase')],
  357. [(b'123 \t ', b'3', b'45 \0 '), (b'', b'', b'UPPER')]]
  358. assert_(issubclass(P.dtype.type, np.string_))
  359. assert_array_equal(P, tgt)
  360. def test_rsplit(self):
  361. A = self.A.rsplit(b'3')
  362. tgt = [[[b' abc '], [b'']],
  363. [[b'12', b'45'], [b'MixedCase']],
  364. [[b'12', b' \t ', b'45 \x00 '], [b'UPPER']]]
  365. assert_(issubclass(A.dtype.type, np.object_))
  366. assert_equal(A.tolist(), tgt)
  367. def test_rstrip(self):
  368. assert_(issubclass(self.A.rstrip().dtype.type, np.string_))
  369. tgt = [[b' abc', b''],
  370. [b'12345', b'MixedCase'],
  371. [b'123 \t 345', b'UPPER']]
  372. assert_array_equal(self.A.rstrip(), tgt)
  373. tgt = [[b' abc ', b''],
  374. [b'1234', b'MixedCase'],
  375. [b'123 \t 345 \x00', b'UPP']
  376. ]
  377. assert_array_equal(self.A.rstrip([b'5', b'ER']), tgt)
  378. tgt = [[' \u03a3', ''],
  379. ['12345', 'MixedCase'],
  380. ['123 \t 345', 'UPPER']]
  381. assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
  382. assert_array_equal(self.B.rstrip(), tgt)
  383. def test_strip(self):
  384. tgt = [[b'abc', b''],
  385. [b'12345', b'MixedCase'],
  386. [b'123 \t 345', b'UPPER']]
  387. assert_(issubclass(self.A.strip().dtype.type, np.string_))
  388. assert_array_equal(self.A.strip(), tgt)
  389. tgt = [[b' abc ', b''],
  390. [b'234', b'ixedCas'],
  391. [b'23 \t 345 \x00', b'UPP']]
  392. assert_array_equal(self.A.strip([b'15', b'EReM']), tgt)
  393. tgt = [['\u03a3', ''],
  394. ['12345', 'MixedCase'],
  395. ['123 \t 345', 'UPPER']]
  396. assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
  397. assert_array_equal(self.B.strip(), tgt)
  398. def test_split(self):
  399. A = self.A.split(b'3')
  400. tgt = [
  401. [[b' abc '], [b'']],
  402. [[b'12', b'45'], [b'MixedCase']],
  403. [[b'12', b' \t ', b'45 \x00 '], [b'UPPER']]]
  404. assert_(issubclass(A.dtype.type, np.object_))
  405. assert_equal(A.tolist(), tgt)
  406. def test_splitlines(self):
  407. A = np.char.array(['abc\nfds\nwer']).splitlines()
  408. assert_(issubclass(A.dtype.type, np.object_))
  409. assert_(A.shape == (1,))
  410. assert_(len(A[0]) == 3)
  411. def test_swapcase(self):
  412. tgt = [[b' ABC ', b''],
  413. [b'12345', b'mIXEDcASE'],
  414. [b'123 \t 345 \0 ', b'upper']]
  415. assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
  416. assert_array_equal(self.A.swapcase(), tgt)
  417. tgt = [[' \u03c3 ', ''],
  418. ['12345', 'mIXEDcASE'],
  419. ['123 \t 345 \0 ', 'upper']]
  420. assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
  421. assert_array_equal(self.B.swapcase(), tgt)
  422. def test_title(self):
  423. tgt = [[b' Abc ', b''],
  424. [b'12345', b'Mixedcase'],
  425. [b'123 \t 345 \0 ', b'Upper']]
  426. assert_(issubclass(self.A.title().dtype.type, np.string_))
  427. assert_array_equal(self.A.title(), tgt)
  428. tgt = [[' \u03a3 ', ''],
  429. ['12345', 'Mixedcase'],
  430. ['123 \t 345 \0 ', 'Upper']]
  431. assert_(issubclass(self.B.title().dtype.type, np.unicode_))
  432. assert_array_equal(self.B.title(), tgt)
  433. def test_upper(self):
  434. tgt = [[b' ABC ', b''],
  435. [b'12345', b'MIXEDCASE'],
  436. [b'123 \t 345 \0 ', b'UPPER']]
  437. assert_(issubclass(self.A.upper().dtype.type, np.string_))
  438. assert_array_equal(self.A.upper(), tgt)
  439. tgt = [[' \u03a3 ', ''],
  440. ['12345', 'MIXEDCASE'],
  441. ['123 \t 345 \0 ', 'UPPER']]
  442. assert_(issubclass(self.B.upper().dtype.type, np.unicode_))
  443. assert_array_equal(self.B.upper(), tgt)
  444. def test_isnumeric(self):
  445. def fail():
  446. self.A.isnumeric()
  447. assert_raises(TypeError, fail)
  448. assert_(issubclass(self.B.isnumeric().dtype.type, np.bool_))
  449. assert_array_equal(self.B.isnumeric(), [
  450. [False, False], [True, False], [False, False]])
  451. def test_isdecimal(self):
  452. def fail():
  453. self.A.isdecimal()
  454. assert_raises(TypeError, fail)
  455. assert_(issubclass(self.B.isdecimal().dtype.type, np.bool_))
  456. assert_array_equal(self.B.isdecimal(), [
  457. [False, False], [True, False], [False, False]])
  458. class TestOperations:
  459. def setup_method(self):
  460. self.A = np.array([['abc', '123'],
  461. ['789', 'xyz']]).view(np.chararray)
  462. self.B = np.array([['efg', '456'],
  463. ['051', 'tuv']]).view(np.chararray)
  464. def test_add(self):
  465. AB = np.array([['abcefg', '123456'],
  466. ['789051', 'xyztuv']]).view(np.chararray)
  467. assert_array_equal(AB, (self.A + self.B))
  468. assert_(len((self.A + self.B)[0][0]) == 6)
  469. def test_radd(self):
  470. QA = np.array([['qabc', 'q123'],
  471. ['q789', 'qxyz']]).view(np.chararray)
  472. assert_array_equal(QA, ('q' + self.A))
  473. def test_mul(self):
  474. A = self.A
  475. for r in (2, 3, 5, 7, 197):
  476. Ar = np.array([[A[0, 0]*r, A[0, 1]*r],
  477. [A[1, 0]*r, A[1, 1]*r]]).view(np.chararray)
  478. assert_array_equal(Ar, (self.A * r))
  479. for ob in [object(), 'qrs']:
  480. with assert_raises_regex(ValueError,
  481. 'Can only multiply by integers'):
  482. A*ob
  483. def test_rmul(self):
  484. A = self.A
  485. for r in (2, 3, 5, 7, 197):
  486. Ar = np.array([[A[0, 0]*r, A[0, 1]*r],
  487. [A[1, 0]*r, A[1, 1]*r]]).view(np.chararray)
  488. assert_array_equal(Ar, (r * self.A))
  489. for ob in [object(), 'qrs']:
  490. with assert_raises_regex(ValueError,
  491. 'Can only multiply by integers'):
  492. ob * A
  493. def test_mod(self):
  494. """Ticket #856"""
  495. F = np.array([['%d', '%f'], ['%s', '%r']]).view(np.chararray)
  496. C = np.array([[3, 7], [19, 1]])
  497. FC = np.array([['3', '7.000000'],
  498. ['19', '1']]).view(np.chararray)
  499. assert_array_equal(FC, F % C)
  500. A = np.array([['%.3f', '%d'], ['%s', '%r']]).view(np.chararray)
  501. A1 = np.array([['1.000', '1'], ['1', '1']]).view(np.chararray)
  502. assert_array_equal(A1, (A % 1))
  503. A2 = np.array([['1.000', '2'], ['3', '4']]).view(np.chararray)
  504. assert_array_equal(A2, (A % [[1, 2], [3, 4]]))
  505. def test_rmod(self):
  506. assert_(("%s" % self.A) == str(self.A))
  507. assert_(("%r" % self.A) == repr(self.A))
  508. for ob in [42, object()]:
  509. with assert_raises_regex(
  510. TypeError, "unsupported operand type.* and 'chararray'"):
  511. ob % self.A
  512. def test_slice(self):
  513. """Regression test for https://github.com/numpy/numpy/issues/5982"""
  514. arr = np.array([['abc ', 'def '], ['geh ', 'ijk ']],
  515. dtype='S4').view(np.chararray)
  516. sl1 = arr[:]
  517. assert_array_equal(sl1, arr)
  518. assert_(sl1.base is arr)
  519. assert_(sl1.base.base is arr.base)
  520. sl2 = arr[:, :]
  521. assert_array_equal(sl2, arr)
  522. assert_(sl2.base is arr)
  523. assert_(sl2.base.base is arr.base)
  524. assert_(arr[0, 0] == b'abc')
  525. def test_empty_indexing():
  526. """Regression test for ticket 1948."""
  527. # Check that indexing a chararray with an empty list/array returns an
  528. # empty chararray instead of a chararray with a single empty string in it.
  529. s = np.chararray((4,))
  530. assert_(s[[]].size == 0)