numpy / numpy/numpy

numpy.genfromtxt raises StopIteration error on empty file

Open
#14,225 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

I'm processing a number of csvs in a single script, and sometimes csvs will be empty, which causes a StopIteration exception. This is not serious, but I think it would be helpful if one or more of the following happened:

  1. This simply be documented: people should know that this happens so they can wrap the section in a try catch block
  2. The exception should be caught and re-passed with a more intuitive name, something like InvalidFile
  3. A new flag should be added/old flag expanded to optionally silence the exception and return an empty array
Reproducing code example:
import numpy as np
import os
inFile = "/tmp/example.csv"
os.system(f"touch {inFile}")
my_data = np.genfromtxt(inFile, delimiter=',', skip_header=1, invalid_raise=false)
Error message:

Traceback (most recent call last):
File "", line 1, in
File "/#####/lib/python3.6/site-packages/numpy/lib/npyio.py", line 1780, in genfromtxt
next(fhd)
StopIteration

Numpy/Python version information:

1.17.0 3.6.3 (default, Apr 24 2018, 18:36:28)
[GCC 4.7.2]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with numpy/lib/npyio.py at genfromtxt, where the traceback shows next(fhd) raising StopIteration. Reproduce the behavior using an empty file and the example options; the issue does not establish whether the desired outcome is documentation, a different exception, or an empty array, so acceptance criteria need to be clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.