From 9c7689794cf59fc0065da8bba1d97002c7be14e3 Mon Sep 17 00:00:00 2001 From: Warren Weckesser Date: Fri, 15 Aug 2014 19:24:37 -0400 Subject: [PATCH] BUG: io: genfromtxt did not handle filling_values=0 correctly. Closes gh-2317. --- numpy/lib/npyio.py | 4 +++- numpy/lib/tests/test_io.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 0e49dd31c343..21d98efe7151 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1518,7 +1518,9 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # Process the filling_values ............................... # Rename the input for convenience - user_filling_values = filling_values or [] + user_filling_values = filling_values + if user_filling_values is None: + user_filling_values = [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 03e238261d99..40a229d14d55 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1300,6 +1300,16 @@ def test_user_filling_values(self): ctrl = np.array([(0, 3), (4, -999)], dtype=[(_, int) for _ in "ac"]) assert_equal(test, ctrl) + data2 = "1,2,*,4\n5,*,7,8\n" + test = np.genfromtxt(TextIO(data2), delimiter=',', dtype=int, + missing_values="*", filling_values=0) + ctrl = np.array([[1, 2, 0, 4], [5, 0, 7, 8]]) + assert_equal(test, ctrl) + test = np.genfromtxt(TextIO(data2), delimiter=',', dtype=int, + missing_values="*", filling_values=-1) + ctrl = np.array([[1, 2, -1, 4], [5, -1, 7, 8]]) + assert_equal(test, ctrl) + def test_withmissing_float(self): data = TextIO('A,B\n0,1.5\n2,-999.00') test = np.mafromtxt(data, dtype=None, delimiter=',',