Skip to content

numpy.genfromtxt raises StopIteration error on empty file #14225

@ambersaw

Description

@ambersaw

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions