summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorgfyoung <gfyoung17@gmail.com>2016-12-06 14:48:03 -0500
committergfyoung <gfyoung17@gmail.com>2016-12-08 12:40:39 -0500
commitdf00a6fa1afca742288a75c7b883f901668e0619 (patch)
treec040c8d315a812a7f00c471dfb74ee7b883917ee /numpy
parent62694d219bd4c768bcd93b85257298bd656817c9 (diff)
downloadnumpy-df00a6fa1afca742288a75c7b883f901668e0619.tar.gz
BUG: Apply more robust string converts in loadtxt
The original dtype converters for bytes and str did not account for converting objects of str or bytes dtype respectively. Replace the original converters with those from numpy.compat, which are much more robust. Closes gh-8033.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/npyio.py4
-rw-r--r--numpy/lib/tests/test_io.py9
2 files changed, 11 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 05010a2d0..13eb5cefc 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -702,9 +702,9 @@ def _getconv(dtype):
elif issubclass(typ, np.complex):
return lambda x: complex(asstr(x))
elif issubclass(typ, np.bytes_):
- return bytes
+ return asbytes
else:
- return str
+ return asstr
def loadtxt(fname, dtype=float, comments='#', delimiter=None,
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 333891d46..61c9c0636 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -686,6 +686,15 @@ class TestLoadTxt(TestCase):
dtype=dt)
assert_array_equal(x, a)
+ def test_str_dtype(self):
+ # see gh-8033
+ c = ["str1", "str2"]
+
+ for dt in (str, np.bytes_):
+ a = np.array(["str1", "str2"], dtype=dt)
+ x = np.loadtxt(c, dtype=dt)
+ assert_array_equal(x, a)
+
def test_empty_file(self):
with suppress_warnings() as sup:
sup.filter(message="loadtxt: Empty input file:")