summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/npyio.py7
-rw-r--r--numpy/lib/tests/test_io.py23
2 files changed, 30 insertions, 0 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index b5723dee5..3b7b3bb30 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1062,6 +1062,13 @@ def _read(fname, *, delimiter=',', comment='#', quote='"',
arr = _ensure_ndmin_ndarray(arr, ndmin=ndmin)
+ if arr.shape[0] == 0:
+ warnings.warn(
+ f'loadtxt: input contained no data: "{fname}"',
+ category=UserWarning,
+ stacklevel=2
+ )
+
if unpack:
# Handle unpack like np.loadtxt.
# XXX Check interaction with ndmin!
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 7cc1b7ede..4a5cc1ad8 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -3242,3 +3242,26 @@ def test_loadtxt_consecutive_quotechar_escaped():
expected = np.array('Hello, my name is "Monty"!', dtype="U40")
res = np.loadtxt(txt, dtype="U40", delimiter=",", quotechar='"')
assert_equal(res, expected)
+
+
+@pytest.mark.parametrize("data", ("", "\n\n\n", "# 1 2 3\n# 4 5 6\n"))
+@pytest.mark.parametrize("ndmin", (0, 1, 2))
+def test_loadtxt_warn_on_no_data(data, ndmin):
+ """Check that a UserWarning is emitted when no data is read from input."""
+ txt = TextIO(data)
+ with pytest.warns(UserWarning, match="input contained no data"):
+ np.loadtxt(txt, ndmin=ndmin)
+
+ with NamedTemporaryFile(mode="w") as fh:
+ fh.write(data)
+ fh.seek(0)
+ with pytest.warns(UserWarning, match="input contained no data"):
+ np.loadtxt(txt, ndmin=ndmin)
+
+
+@pytest.mark.parametrize("skiprows", (2, 3))
+def test_loadtxt_warn_on_skipped_data(skiprows):
+ data = "1 2 3\n4 5 6"
+ txt = TextIO(data)
+ with pytest.warns(UserWarning, match="input contained no data"):
+ np.loadtxt(txt, skiprows=skiprows)